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
Related
This question already has answers here:
Type Checking: typeof, GetType, or is?
(15 answers)
Checking if the object is of same type
(4 answers)
How can I check if an object is of a certain type at runtime in C#?
(9 answers)
Closed 3 years ago.
I was wondering if there is a way to find if my interface reference is a specific class.
For example i have DeviceInterface reference, and Playstation, PC and Mac all implement it. Is there a way to see if DeviceInterface is a PC?
I have thought about using a enum to define the type and using that, but is there a way of avoiding this and using a type check or something along those lines?
Thanks in advance.
Let's say that you have
DeviceInterface PcDevice = new PC();
In that case you can just do:
if (PcDevice is PC) { console.WriteLine("I'm a PC"); }
read more here to understand the is and as operators better
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:
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?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why are the properties of anonymous types in C# read-only?
I wrote something like this,
var suspense = new { Name = "Android", Market = string.Empty };
suspense.Market = "Potential";
.NET throws error
Property or indexer 'AnonymousType#1.Market' cannot be assigned to --
it is read only
I know that AnonymousTypes in C# are immutable, but why? Is this due to some limitation with CLR?
The motivating factor for driving the immutable anonymous types was because the LINQ APIs used hash tables internally and returning projections of anonymous types that could be modified was a dangerous situation.
You can check :
Immutable types: understand their benefits and use them
Anonymous Types and Object Identities By Tim Ng on MSDN
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “DateTime?” mean in C#?
What does the ? mean after a type?
I had a function declaration including a questionmark after the datatype like:
private TimeSpan? sometime()
{
}
What does this mean?
TimeSpan? is shorthand for System.Nullable<TimeSpan>.
A TimeSpan is a value type, which cannot take a null value. By wrapping it in a System.Nullable<> it can be null. Without that ? it would be illegal to return null from the function.
Nullable Structure
Represents an object whose underlying type is a value type that can
also be assigned null like a reference type.
Instead of writing Nullable<TimeSpan>, you can write TimeSpan?.
Nullable, that a value type can be null.
It means that the value type is a nullable type
Basically its a nullable TimeStamp.