I am working in a small educational project where we have to implement a n-dimensional matrix. Depending on the context, this matrix has either to work with our own built-in ComplexNumber struct or with System.Double and for very simple examples, with integral types (mainly System.Int32).  Due to the nature of the application, we are not demanded to implement lightning fast performance.   My first idea was therefore to implement a Matrix&lt;T&gt; where T would somehow need to be restricted to "numbers".  The obvious problem in doing this is that there is no way right now in the language to constraint the generic type T with defined operators. Also I do not see an easy way to restrcit T to reasonable types.  My questions are:    Can somebody please point me in the direction of an elegant way to make mathematical operations with generic types that doesn't compromise performance too much and to somehow make that work with built in types (if possible). If Eric ever reads this, does this feature (constraining generic types by defined operators) ever come up in hypothetical future versions of C# design meetings and has it ever been close to making it into the language?   I know its easier and better to implement a ComplexMatrix type and create wrappers around it for each matrix "sub-type" (double, integral, etc.) and pay the performance costs of the conversions between our complex type and whichever type the matrix elements happen to be. This question is more out of curiousity on how would someone implement a similar scenario.
12964
Can anyone tell me if there is a way with generics to limit a generic type argument T to only:   Int16 Int32 Int64 UInt16 UInt32 UInt64   I'm aware of the where keyword, but can't find an interface for only these types,  Something like:  static bool IntegerFunction&lt;T&gt;(T value) where T : INumeric
102675
I have a little game written in C#. It uses a database as back-end. It's  a , and I wanted to implement the function of the cards as a script.  What I mean is that I essentially have an interface, ICard, which a card class implements (public class Card056: ICard) and which contains a function that is called by the game.  Now, to make the thing maintainable/moddable, I would like to have the class for each card as source code in the database and essentially compile it on first use. So when I have to add/change a card, I'll just add it to the database and tell my application to refresh, without needing any assembly deployment (especially since we would be talking about 1 assembly per card which means hundreds of assemblies).  Is that possible? Register a class from a source file and then instantiate it, etc.  ICard Cards[current] = new MyGame.CardLibrary.Card056(); Cards[current].OnEnterPlay(ref currentGameState);   The language is C# but extra bonus if it's possible to write the script in any .NET language.
115478
I am using C#, Entity Framework. SQL Server for the development. I have a method which has two parameters (int ID, List&lt;String&gt; _listobj). For each ID, there is a list of obj. All of these IDs and lists will be inserted into different tables in database. So the question is: what's the best practice for this? I have couple of options:    In C#, split the list&lt;obj&gt; as single value, then create a stored procedure which takes two parameters (ID, str), then the C# will call the stored procedure many times depending on the size of the list Pass the ID and list&lt;obj&gt; as two parameters to the stored procedure and let the stored procedure split the list&lt;obj&gt;. This way C# will only call the stored procedure once   Is the second strategy doable? If yes, which is better in terms of performance?
23054
Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations?  This is what I want to re-write:  foreach (Type t in this.GetType().Assembly.GetTypes())     if (t is IMyInterface)         ; //do stuff
44787
If you are creating a 1d array, you can implement it as a List, or else use the 'array' module in the standard library.  I have always used Lists for 1d arrays.  What is the reason or circumstance where I would want to use the array module instead?  Is it for performance and memory optimization, or am I missing something obvious?
26446
I didn't attend PDC 2008, but I heard some news that C# 4.0 is announced to support Generic covariance and contra-variance. That is, List&lt;string&gt; can be assigned to List&lt;object&gt;. How could that be?  In Jon Skeet's book C# in Depth, it is explained why C# generics doesn't support covariance and contra-variance. It is mainly for writing secure code. Now, C# 4.0 changed to support them. Would it bring chaos?  Anybody know the details about C# 4.0 can give some explanation?
63486
Say you need to have a list/array of integers which you need iterate frequently, and I mean extremely often. The reasons may vary, but say it's in the heart of the inner most loop of a high volume processing.  In general, one would opt for using Lists (List) due to their flexibility in size. On top of that, msdn documentation claims Lists use an array internally and should perform just as fast (a quick look with Reflector confirms this). Neverless, there is some overhead involved.  Did anyone actually measure this? would iterating 6M times through a list take the same time as an array would?
63400
Seeing as C# can't switch on a Type (which I gather wasn't added as a special case because is relationships mean that more than one distinct case might apply), is there a better way to simulate switching on type other than this?  void Foo(object o) {     if (o is A)     {         ((A)o).Hop();     }     else if (o is B)     {         ((B)o).Skip();     }     else     {         throw new ArgumentException("Unexpected type: " + o.GetType());     } }
3552
According to the documentation of the == operator in ,      For predefined value types, the   equality operator (==) returns true if   the values of its operands are equal,   false otherwise. For reference types   other than string, == returns true if   its two operands refer to the same   object. For the string type, ==   compares the values of the strings.   User-defined value types can overload   the == operator (see operator). So can   user-defined reference types, although   by default == behaves as described   above for both predefined and   user-defined reference types.   So why does this code snippet fail to compile?  bool Compare&lt;T&gt;(T x, T y) { return x == y; }   I get the error Operator '==' cannot be applied to operands of type 'T' and 'T'. I wonder why, since as far as I understand the == operator is predefined for all types?  Edit: Thanks, everybody. I didn't notice at first that the statement was about reference types only. I also thought that bit-by-bit comparison is provided for all value types, which I now know is not correct.  But, in case I'm using a reference type, would the == operator use the predefined reference comparison, or would it use the overloaded version of the operator if a type defined one?  Edit 2: Through trial and error, we learned that the == operator will use the predefined reference comparison when using an unrestricted generic type. Actually, the compiler will use the best method it can find for the restricted type argument, but will look no further. For example, the code below will always print true, even when Test.test&lt;B&gt;(new B(), new B()) is called:  class A { public static bool operator==(A x, A y) { return true; } } class B : A { public static bool operator==(B x, B y) { return false; } } class Test { void test&lt;T&gt;(T a, T b) where T : A { Console.WriteLine(a == b); } }
63337
Is there a short way of converting a strongly typed List&lt;T&gt; to an Array of the same type, e.g.: List&lt;MyClass&gt; to MyClass[]?  By short i mean one method call, or at least shorter than:  MyClass[] myArray = new MyClass[list.Count]; int i = 0; foreach (MyClass myClass in list) {     myArray[i++] = myClass; }
2552
Possible Duplicate:          I've coded something like this:  public bool IsDataChanged() {                T value1 = GetValue2;     T value2 = GetValue1();      return (valueInDB != valueFromView); }   Right now the function doesn't compile with the error "Operator '!=' cannot be applied to operands of type 'T' and 'T'". What do I have to do to make this function work ?
40034
Coming from C++ to Java, the obvious unanswered question is why didn't Java include operator overloading?  Isn't Complex a, b, c; a = b + c; much simpler than Complex a, b, c; a = b.add(c);?  Is there a known reason for this, valid arguments for not allowing operator overloading? Is the reason arbitrary, or lost to time?
69582
Here's what I've been trying to do, in a nutshell:  class example &lt;T extends Number&gt; {     private int function(T number)     {         int x = (int) number;         ...     }     ... }   Basically, I'm trying to make it so that T is a number so I can convert it to an int inside that function. The problem is that I'm getting an "incovertible types" error, so I must be doing something wrong.
30163
What is the difference between this:  void MyMethod(IMyInterface value) {     //... }   and this:  void MyMethod&lt;T&gt;(T value) where T : IMyInterface {     //... }
43660
I have a list of structs and I want to change one element. For example :  MyList.Add(new MyStruct("john"); MyList.Add(new MyStruct("peter");   Now I want to change one element:  MyList[1].Name = "bob"   However, whenever I try and do this I get the following error:     Cannot modify the return value of   System.Collections.Generic.List.this[int]‘ because it is not   a variable   If I use a list of classes, the problem doesn't occur.  I guess the answer has to do with structs being a value type.  So, if I have a list of structs should I treat them as read-only? If I need to change elements in a list then I should use classes and not structs?
6902
Let's say I have a generic member in a class or method, like so: public class Foo&lt;T&gt; {     public List&lt;T&gt; Bar { get; set; }          public void Baz()     {         // get type of T     }    }  When I instantiate the class, the T becomes MyTypeObject1, so the class has a generic list property: List&lt;MyTypeObject1&gt;. The same applies to a generic method in a non-generic class: public class Foo {     public void Bar&lt;T&gt;()     {         var baz = new List&lt;T&gt;();                  // get type of T     } }  I would like to know what type of objects the list of my class contains. So what type of T does the list property called Bar or the local variable baz contain? I cannot do Bar[0].GetType(), because the list might contain zero elements. How can I do it?
49954
In C#, when I have an interface and several concrete implementations, can I cast the interface to a concrete type or is concrete type cast to interface?  What are the rules in this case?