C# 2.0 - Generics

  • Thread starter Thread starter Pierre Y.
  • Start date Start date
Pierre Y. said:
Hi,

I've just read that article :

http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp

I'm asking something. Why are generics constraints not been implemented
using Attributes instead of "where..." syntax ?

AFAIK, attributes are pretty much ignored by the compiler (other than
emitting them as metadata), whereas constraints are very definately an
instruction to the compiler. We could theoretically put inheritance
instructions in attributes:
[InheritsFrom(typeof(System.MarshalByRefObject))]
public class MyClass {...}

but it wouldn't look as clean, and it would break the code/metadata
difference.
 
Sean Hederman avait soumis l'idée :
Pierre Y. said:
Hi,

I've just read that article :

http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp

I'm asking something. Why are generics constraints not been implemented
using Attributes instead of "where..." syntax ?

AFAIK, attributes are pretty much ignored by the compiler (other than
emitting them as metadata), whereas constraints are very definately an
instruction to the compiler. We could theoretically put inheritance
instructions in attributes:
[InheritsFrom(typeof(System.MarshalByRefObject))]
public class MyClass {...}

but it wouldn't look as clean, and it would break the code/metadata
difference.

Thank you. I think I have understand in the above paper that generics
are supported by IL/CLR. At runtime, JIT compiler produces native code
and performs type and constraints checks.

So generics types and constraints are metadatas too ?

Correct me If I'm wrong please.

Regards,

Pierre Y.
 
Thank you. I think I have understand in the above paper that generics are
supported by IL/CLR. At runtime, JIT compiler produces native code and
performs type and constraints checks.

So generics types and constraints are metadatas too ?

Yes, constraints are stored as metadata, if you want to be techincal,
everything else about a method(its return type, its parameters, etc) are
metadata as well. Its the metadata that makes .NET really interesting.

There are very few attributes that affect compilation... and they rarely
change how code is interpreted(Conditional skips method calls, DllImport may
cause a different instruction to be generated, I can't recall of the top of
my head).

While constraints are recorded in metadata, they still change the way the
code within the method is understood, and thus are code, not metadata.
 
Back
Top