Click here to Skip to main content
15,886,362 members
Home / Discussions / C#
   

C#

 
AnswerRe: Opening .xls file in Excel from C# Pin
Gareth H11-May-08 4:41
Gareth H11-May-08 4:41 
AnswerRe: Opening .xls file in Excel from C# Pin
Ed.Poore11-May-08 9:46
Ed.Poore11-May-08 9:46 
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 
George_George wrote:
For 1, from your comments, I think it is also a common feature for any non-generics types -- compile to native code (JIT) only once when the type is used 1st time. The article should mentioned something special for generics?


Let's say you have the following piece of code

class Gen<T>
{
}


and then you do Gen<int> val = new Gen<int>();. In C#, at runtime, the CLR knows that val is an instance of Gen<int>. You can check it out yourself - using reflection, you can get the generic type (Gen) of val. With templates in C++, the compiler generates a new class at the source code level, with the "expanded" code, and val will then be an instance of that autogenerated class. The fact that val is an instance of a template class is lost at runtime.

I guess the key thing to remember is that in .NET, generic type information is preserved at compile time and is available at runtime.

George_George wrote:
For 2, "In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. C++ does allow template parameters." You mentioned "Constructed types are those that have been JIT'ed.", but what are not JITed (in your points not allowed)? I think during runtime, every type is JITed.


In the above example, Gen<int> is a constructed type - you've "constructed" a type using the generic Gen class. This[^] has a pretty good explanation for constructed types.

The statement that you mentioned means that you can't have generic generic type parameters - unlike C++, which allows template template types as template parameters.


George_George wrote:
For 3, I do not know the differences, both C++ and C# will report compile error other than runtime error when met with some unsupported operator on a specific input parameter type, right?


Yes, but the way they work is different. C++, like I said before, generates a new class every time you instantiate a template class with a different type parameter. After that, it just compiles the autogenerated class as part of the source code and you'll get compile errors if the type you provided as the template parameter doesn't have the expected method/field/whatever that the template class expected.

In .NET, the generic type can be part of a different assembly from the code attempting to use it, so obviously, the C++ approach won't work. Therefore, the C# compiler forces you to declare constraints, which let the compiler know which operations are supported. The constraints are compiled into the assembly, and when somebody passes a type that doesn't satisfy the constraint, the compiler can figure that out and report a compiler error.

Hope that helps.

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

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 
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 

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.