How does Visual Studio decide Nullable<T> is not a struct? [duplicate] - c#

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.

Related

Nullable reference types in C# 8.0 [duplicate]

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?

Why is Object.ReferenceEquals(null of a nullable primitive) true? [duplicate]

This question already has answers here:
How is the boxing/unboxing behavior of Nullable<T> possible?
(3 answers)
Why does Nullable<T> HasValue property not throw NullReferenceException on Nulls?
(5 answers)
Closed 4 years ago.
So we can have nullable primitive types
bool? x = null;
Which is great. But my understanding has always been that it's not actually a null reference since
x.HasValue; //false
is possible.
I had always assumed that nullables were classes or structures with operator and Equals overloading implemented but today I checked
Object.ReferenceEquals(null, x); //true
And I have no clue how that can be.
How can it at the same time be a reference that points to null and have non-extension properties on it that are invokable?!
If this is a compiler trick, does anyone know documentation on what exactly it does?

Compile error when assigning IQueryable<int> to IQueryable<object> [duplicate]

This question already has answers here:
Why covariance and contravariance do not support value type
(4 answers)
Closed 7 years ago.
Why the compiler doesn't allow to assign ints to objects?
IQueryable<object> objects = null;
IQueryable<int> ints = null;
objects = ints;
Implicit type conversion (a function of covariance) does not apply to all generics. SomeGeneric<ValueType> is not derived from to SomeGeneric<Reference> and thus, it is not valid to cast it even if there is a already an implicit conversion for the type parameters (in this case, boxing).
If you are in C# 4.0, a generic interface can be defined as covariant using ISomeGeneric<out T> and provided that the generic arguments are derived from one another, then you can cast. If the generic arguments are not derived, it is not possible to cast.

What does <T?> mean in Class declaration? [duplicate]

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.

Difficulty in understanding class Property declaration [duplicate]

This question already has answers here:
What does the bool? return type mean?
(3 answers)
A curious C# syntax with a question mark
(3 answers)
Closed 9 years ago.
I was going through a class library in one of our projects.
i came across statements like public int? variablename
What does this mean..I am going to create classes for new application refering current
application .
So ,i would like to know what it is and in which scenarios we can use and is it not
possible to avoid it
int? means that it is "nullable". Being nullable allows you to have null values in your int.
Check out this link if you need to understand a bit more,
http://msdn.microsoft.com/en-us/library/2cf62fcy%28VS.80%29.aspx
How do nullable types work in C#?
The question mark is a shortcut for Nullable<> (MSDN).
int? is the same as Nullable<int>
That is a nullable type msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx
This means that the variable is nullable

Categories