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?
Related
This question already has answers here:
Why "long value" equals to null is allowed?
(3 answers)
How does comparison operator works with null int?
(5 answers)
Closed 7 years ago.
I've always used the coalescing operator (a.k.a. "really surprised operator, i.e. "??") to get rid of phony nullables (usually fetched from DB as allowing nulls but known to me never to be at that value). It looks like so.
int serious = GetSomeReallyNonNullValue();
int? phony = GetNullableButActuallyNonNullValue();
int result = serious + (phony ?? 0);
However, I just noticed that the below actually compiled. Can't see how it makes sense. And I can't see intuitively if null value will evaluate the expression to true or false...
int? test = null;
if (test < 1337) ;
A lot has been written about "lifting" operations in C# (eg. here), where operators with Nullable<T> arguments are treated as operators on T when all operands are non-null. And null is only equivalent to itself.
usually fetched from DB as allowing nulls but known to me never to be at that value
In which case why is the column not set to not null?
The lifting is there because so many databases have nullable columns when they should not be.
This question already has answers here:
C# okay with comparing value types to null
(11 answers)
Closed 7 years ago.
Here is some example code:
static DateTime time;
if (time == null)
{
/* do something */
}
Since DateTime cannot be null, why does this code compile?
Edit:
The issue is not just that this code will always return false,but why something like DateTime which is never null is allowed in such a comparison.
Although time is of a non-nullable value type, it can be converted to nullable and compared to null. The comparison would yield false, which is a valid outcome.
This does not mean, however, that it is a good code. Tools, such as re:sharper, would flag this line with a warning saying "Expression is always false".
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
This question already has answers here:
Direct casting vs 'as' operator?
(16 answers)
Casting vs using the 'as' keyword in the CLR
(18 answers)
What is the difference between casting and using "as" in C#?
(8 answers)
Closed 10 years ago.
Assume, that there is an interface A and a variable 'x' of a class that implements that interface. Now I can perform these:
var a = (A) x;
Or:
var a = x as A;
I know that in the case of failure, the first statement will throw an InvalidCastException and the second will return null. But is there any other difference? Particurarly in performance?
By doing (A)x you are doing a cast which will definatly try and cast, if it can't cast there will be an exception.
If you use as it will either cast or be null.
However, you have all the example code you need to try this yourself so you could of potentially tried this before asking us what the code you have stated is going to do.
The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.
You can use the as operator to perform certain types of conversions between compatible reference types or nullable types.
Consider the following example:
expression as type
The code is equivalent to the following expression except that the expression variable is evaluated only one time.
expression is type ? (type)expression : (type)null
Note that the as operator performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.
Refer:
as (C# Reference)
The first one attempts to convert immediately, the second one actually checks whether x is of type A.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Direct casting vs 'as' operator?
Firstly I am sorry if this is a duplicate, I have tried to google but it is not an easy "google to do"!
Is there a difference (functionally, performance wise, etc) between the following code fragments:
MyClass myClass = (MyClass)someObject;
MyClass myClass = someObject as MyClass;
Yes there is a difference!
as operator will set variable to null if casting fails
Explicit casting will raise exception
Yes, the first variant will throw an exception, if it can't cast 'someObject' to 'MyClass'. Whereas the second will then just return null.