Click here to Skip to main content
15,885,546 members
Home / Discussions / C#
   

C#

 
AnswerRe: Opening .xls file in Excel from C# Pin
Jimmanuel11-May-08 15:05
Jimmanuel11-May-08 15:05 
QuestionSystem Error mgs and flickering issue.... Pin
ASysSolvers11-May-08 3:03
ASysSolvers11-May-08 3:03 
Questiongenerics question Pin
George_George10-May-08 22:35
George_George10-May-08 22:35 
AnswerRe: generics question Pin
carbon_golem11-May-08 7:25
carbon_golem11-May-08 7:25 
GeneralRe: generics question Pin
George_George11-May-08 15:18
George_George11-May-08 15:18 
GeneralRe: generics question Pin
S. Senthil Kumar11-May-08 19:31
S. Senthil Kumar11-May-08 19:31 
GeneralRe: generics question Pin
George_George11-May-08 21:45
George_George11-May-08 21:45 
GeneralRe: generics question Pin
S. Senthil Kumar11-May-08 22:45
S. Senthil Kumar11-May-08 22:45 
1. You are passing a constructed type (Generic2<List<int>>) and not the generic type itself. It's critical that you understand the difference between the two. Try compiling Generic<Generic2>. Does it compile? Can you see how that's different from your code?

George_George wrote:
Means a genetics type defined in one assembly, but used/referred from another assembly? So, C# can do binary level (IL) reference, but C++ could only do source code level (header file) reference?


Yes, generic types can be used from a different assembly. Most template code in C++ is consumed as header files (STL, for example). IIRC, standard C++ has some facility to export templates, but none of the popular compilers support it, AFAIK.


George_George wrote:
Therefore, the C# compiler forces you to declare constraints" -- we can define a generics type without defining any constraints, right?

In what situations, do we have to define constraints when define a generics type?


Without defining constraints, you can't call any methods/properties on the generic member/variable except those defined on System.Object (because all instances are guaranteed to be derived from System.Object). Try this code, for example
class G<T>
{
   T t;
   public G(T t)
   {
     this.t = t;
   }

   public void Method()
   {
      t.SayHello();
   }
}

interface IHello
{
   void SayHello();
}

class Hello : IHello
{
   public void SayHello() { Console.WriteLine("Hello"); }
}

static void Main()
{
   Hello h = new Hello();
   G<Hello> g = new G<Hello>(h);
   g.Method();
}


You'll find that this code doesn't compile. Let's say that G is defined in assembly A and the rest of the code is in assembly B. The compiler, when compiling code for assembly A, cannot figure out by itself that Method in G calls SayHello() on the generic member and therefore that the instance passed to G's constructor must have method SayHello. It therefore assumes only what it knows - that T must be derived from System.Object.

To get the code to compile, you'll have to tell the compiler that T will have to have SayHello. You can do this by specifying a constraint - in this case, I tell it that T is or is derived from the interface IHello. IHello has SayHello as an interface method, so the compiler knows that the generic member will have the method SayHello, and so the the generic class will compile fine.

class G<T> where T : IHello
{...}


Now when you construct G<Hello>, the compiler checks if Hello is/inherits from IHello and because it does, the code compiles fine.

Does that help?

Regards
Senthil [MVP - Visual C#]
_____________________________
My Blog | My Articles | My Flickr | WinMacro

GeneralRe: generics question Pin
George_George11-May-08 23:06
George_George11-May-08 23:06 
GeneralRe: generics question Pin
S. Senthil Kumar11-May-08 23:59
S. Senthil Kumar11-May-08 23:59 
GeneralRe: generics question Pin
George_George12-May-08 1:28
George_George12-May-08 1:28 
GeneralRe: generics question Pin
S. Senthil Kumar12-May-08 2:17
S. Senthil Kumar12-May-08 2:17 
GeneralRe: generics question Pin
George_George12-May-08 2:32
George_George12-May-08 2:32 
QuestionFile header processing... [modified] Pin
natsuyaki10-May-08 22:00
natsuyaki10-May-08 22:00 
AnswerRe: File header processing... Pin
DanB198310-May-08 23:14
DanB198310-May-08 23:14 
GeneralRe: File header processing... Pin
natsuyaki10-May-08 23:34
natsuyaki10-May-08 23:34 
GeneralRe: File header processing... Pin
DanB198310-May-08 23:52
DanB198310-May-08 23:52 
GeneralRe: File header processing... Pin
natsuyaki10-May-08 23:59
natsuyaki10-May-08 23:59 
AnswerRe: File header processing... Pin
boblaw9911-May-08 4:13
boblaw9911-May-08 4:13 
GeneralRe: File header processing... Pin
natsuyaki11-May-08 4:24
natsuyaki11-May-08 4:24 
AnswerRe: File header processing... Pin
boblaw9911-May-08 5:03
boblaw9911-May-08 5:03 
GeneralRe: File header processing... Pin
natsuyaki11-May-08 5:16
natsuyaki11-May-08 5:16 
AnswerRe: File header processing... Pin
boblaw9911-May-08 5:22
boblaw9911-May-08 5:22 
GeneralRe: File header processing... Pin
natsuyaki11-May-08 5:34
natsuyaki11-May-08 5:34 
Questionmeta data and attribute Pin
George_George10-May-08 21:43
George_George10-May-08 21:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.