This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
Closed 9 years ago.
class MyClass<T> is a generic class. But what is class MyClass<T?> ?
I'm only guessing it defines T as nullable type ? But if its so I don't understand how and why.
Can't find anything about it on web.
No, you've misread. There is no class MyClass<T?>.
There's class MyClass<T> : OtherClass<T?> where T : struct, which is perfectly legal.
In other words, you can do this:
class NullableList<T> : List<T?> where T : struct {}
And you'll get a generic list, which uses a nullable type as its generic type, eg. if you create a NullableList<int>, it will have a method Add(int? val).
Funnily enough, through the syntactic sugar magic of nullables, you can't have a Nullable of a Nullable (eg. Nullable<Nullable<int>> for example), even though Nullable<T> is a struct. It produces a compile-time error.
Related
This question already has answers here:
Ambiguous call between two C# extension generic methods one where T:class and other where T:struct
(3 answers)
Closed 4 months ago.
The compilers tells me that the following two methods have the same signature:
internal static void Foo<T>(T? output) where T : class
{}
internal static void Foo<T>(T output) where T : struct
{}
But whis is this the case? In the first method, the parameter allows for nullable reference types and in the second we only allow for value types. Or to state the question differently, why is T? and T the same in this case?
Generic constraints are not a part of method signature in the C#/CLR. There are some discussions about this in the C# language repo.
This question already has answers here:
C#'s can't make `notnull` type nullable
(2 answers)
Nullable reference types: How to specify "T?" type without constraining to class or struct
(3 answers)
Closed 3 years ago.
C# 8.0 introduced the Nullable reference types:
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#nullable-reference-types
In short, this new feature allows the specification of a reference variable or property to be null, enabling compiler warnings in case of possible null-reference exception.
Anyway, in case of generic types, the developer is forced to specify whether the generic type must be a struct or a reference type. In other words, this would result in a compiler error:
public class Foo<T>
{
public T? Bar { get; set; }
}
[CS8627] A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
My question is: is there a way to make generic parameters work both with struct and reference types? Is it mandatory to write two different implementation for struct and reference types?
This question already has answers here:
Why is Nullable<T> nullable? Why it cannot be reproduced?
(3 answers)
Closed 4 years ago.
Nullable<int> intNullable = null;//this is ok since `Nullable<T> where T : struct`
However Nullable<Nullable<int>> is NOT OK although Nullable<int> is a struct.
How is this possible?
There is special language support for Nullable<T>, in numerous ways, one of which is that Nullable<Nullable<T>> is not valid. You could not create your own custom generic struct that could have any other struct except itself as the generic argument. The generic constraint function of C# just isn't powerful enough to do it.
Also note that the error message does not say that Nullable<int> isn't a struct, it says that it must be a non-nullable struct. Nullable<int> isn't a non-nullable struct. It's a nullable struct.
This question already has answers here:
Can you use "where" to require an attribute in c#?
(5 answers)
Closed 8 years ago.
Is it possible to restrict a generic type to a class that implements a particular attribute?
If I have an attribute called MyCustomAttribute... I want to write something like this:
public class SomeRandomClass<T>
where T : MyCustomAttribute
{
}
but not where T is a derived class of MyCustomAttribute, but a class that implements the attribute.
Thanks
edit:
I mean implemented like this:
[MyCustomAttribute]
public class SomeOtherClass
{
}
If you are looking for generic parameter constraint which will require type argument to be marked with some attribute, then its not possible.
Here is list of possible constraints: Constraints on Type Parameters (C# Programming Guide). You can add following six constraints for type argument
to be a value type
to be a reference type
to have a public parameterless constructor
be or derive from specified base class
implement specified interface
be or derive from other argument
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Nullable type as a generic parameter possible?
Is creating a C# generic method that accepts (nullable) value type and reference type possible?
In c# I would like to restrict my generic method to accept only nullable types. Is that possible?
public T Method<T>() where T : somesortofnullablerestriction
{
...
}
You can do this.
public Nullable<T> Method<T>()
{
}