I am self-studying a C# reference and it gives the following information:
1.21.4. Declaring Generic Parameters Generic parameters can be introduced in the declaration of classes, structs, interfaces, delegates (see the upcoming ‘Delegates’ section), and methods. Other constructs, such as properties, cannot introduce a generic parameter, but can use a generic parameter. For example, the property Value uses T:
public struct Nullable<T> { public T Value {get;} }
First, I get an error trying to compile this saying that it must contain a body because it is neither abstract nor extern or that automatic parameters must have both get and set accessors.
Second, assuming it is wrong and I correct it by adding ‘set;’, I cannot seem to format a call to it successfully.
I’m not sure if you just picked a bad example for your struct name (since Nullable is a framework struct), but if not, the error is due to the fact that you have no set accessor in your property. Automatic properties (added in C# 3.0) need both a get and set property. So, if you change the code to:
it should work. In this case, the error had nothing to do with generics. To create an instance, you could use:
This will make it compile. However, as both Jon and Cerebrus has pointed out, it’s probably just an example to show the workings of generics.