I have (for example) an object of type A that I want to be able to cast to type B (similar to how you can cast an int to a float)
Data types A and B are my own.
Is it possible to define the rules by which this casting occurs?
Example
int a = 1;
float b = (float)a;
int c = (int)b;
...
Is there a way to do the below? Imagine a generic result wrapper class. Where you have a type and an associated error list. When there is no result to return to the user we will use boolean to indicate success failure. I want to create a constructor that takes in an error list, and if the list is null or count 0, AND the type is a b...
Hi All
I'm currently trying to write a Dump() method from LinqPad equivalent iin C# for my own amusment. I'm moving from Java to C# and this is an exercise rather than a business requirement. I've got almost everything working except for Dumping a Dictionary.
The problem is that KeyValuePair is a Value type. For most other Value types ...
I have a convert method, which takes a String and a class as arguments and constructs an object of the given class, which will be returned.
The usage should look like this
Something s = Converter.convert("...", Something.class)
Is it possible to express this with Java generics?
...
Hello, I am really trying to follow the DRY principle. I have a sub that looks like this?
Private Sub DoSupplyModel
OutputLine("ITEM SUMMARIES")
Dim ItemSumms As New SupplyModel.ItemSummaries(_currentSupplyModel, _excelRows)
ItemSumms.FillRows()
OutputLine("")
OutputLine("NUMBERED INVENTORIES")...
If we have it for interfaces, why dont we have it also for classes? What would be the problem that we would incurr when using it?
Thanks
...
When we define our interfaces in C# 4.0, we are allowed to mark each of the generic parameters as in or out. If we try to set a generic parameter as out and that'd lead to a problem, the compiler raises an error, not allowing us to do that.
Question:
If the compiler has ways of inferring what are valid uses for both covariance (out) an...
I'm studying the source code of the Scala 2.8 collection classes. I have questions about the hierarchy of scala.collection.Traversable. Look at the following declarations:
package scala.collection
trait Traversable[+A]
extends TraversableLike[A, Traversable[A]]
with GenericTraversableTemplate[A, Traversable]
tr...
Hi
i am trying to implement a generic method which allows to output csv file
public static void WriteToCsv<T>(List<T> list) where T : new()
{
const string attachment = "attachment; filename=PersonList.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext...
Okay, this might be confusing. What I'm trying to do is use an enumerator to only return certain items in a generic list based on class type.
Given the following hierarchy:
type
TShapeClass = class of TShape;
TShape = class(TObject)
private
FId: Integer;
public
function ToString: string; override;
...
Hi,
I am reading through Jon Skeet's "C# in Depth", first edition (which is a great book). I'm in section 3.3.3, page 84, "Implementing Generics". Generics always confuse me, so I wrote some code to exercise the sample. The code provided is:
using System;
using System.Collections.Generic;
public sealed class Pair<TFirst, TSecond> ...
In F#, given the following class:
type Foo() =
member this.Bar<'t> (arg0:string) = ignore()
Why does the following compile:
let f = new Foo()
f.Bar<Int32> "string"
While the following won't compile:
let f = new Foo()
"string" |> f.Bar<Int32> //The compiler returns the error: "Unexpected type application"
...
Here's the scenario i am faced with:
public abstract class Record { }
public abstract class TableRecord : Record { }
public abstract class LookupTableRecord : TableRecord { }
public sealed class UserRecord : LookupTableRecord { }
public interface IDataAccessLayer<TRecord>
where TRecord : Record { }
public interface ITable...
I've clearly been stuck in Java land for too long... Is it possible to do the C++ equivalent of the following Java code:
interface Foo {}
class Bar implements Foo {}
static List<Foo> getFoo() {
return new LinkedList<Foo>();
}
static List<Bar> getBar() {
return new LinkedList<Bar>();
}
List<? extends Foo> stuff = getBar();
Wher...
I have various ObservableCollections of different object types. I'd like to write a single method that will take a collection of any of these object types and return a new collection where each element is a deep copy of elements in the given collection. Here is an example for a specifc class
private static ObservableCollection<Prop...
I am not sure if the following is either sufficiently complicated or excessively complicated.
It basically boils down to an interrelated class and interface. (See below) Suggestions appreciated....
/**
* @param <T> item type
* @param <TF> table format type
*/
interface EnumTableFormatItem<T, TF extends TableFormat<T>> {
Object g...
I'm a bit confused about how Java generics handle inheritance / polymorphism.
Assume the following hierarchy -
Animal (Parent)
Dog - Cat (Children)
So suppose I have a method doSomething(List<Animal> animals). By all the rules of inheritance and polymorphism, I would assume that a List<Dog> is a List<Animal> and a List<Cat> is a List...
I am serializing a generic dictionary in VB.net and I am very surprised that it is about 1.3kb with a single item. Am I doing something wrong, or is there something else I should be doing? I have a large number of dictionaries and it is killing me to send them all across the wire. The code I use for serialization is
Dim diction...
Simple question, but tricky answer I guess.
Does using Generic Interfaces hurts performance?
Example:
public interface Stuff<T> {
void hello(T var);
}
vs
public interface Stuff {
void hello(Integer var); <---- Integer used just as an example
}
My first thought is that it doesn't. Generics are just part of the language ...
The following code fails to compile, producing a "Widget must be a non-abstract type with a public parameterless constructor" error. I would think that the compiler has all of the information it needs. Is this a bug? An oversight? Or is there some scenario where this would not be valid?
public class Factory<T> where T : new()
{
publ...