This question already has answers here:
Is this pattern matching expression equivalent to not null
(2 answers)
Closed 2 years ago.
So, I think in the current C# version which is C# 8. There are a few ways to check the reference types are not null. I am confused by these two below:
o is object
o is {}
What's the main difference between them, I cannot find much info for them.
Can someone explain it to me? Which one is recommended? Or what are the pro and cons of each one?
o is object is the original way of checking the type of an instance (available since C# 1.0)
o is {} uses the pattern matching with property patterns introduced in C# 8.0
{} denotes an object instance with any (or none) properties, i.e. any object instance.
Related
This question already has answers here:
Why C# 6.0 doesn't let to set properties of a non-null nullable struct when using Null propagation operator?
(2 answers)
Why some types do not have literal modifiers
(6 answers)
Closed 2 years ago.
I just noticed that property assignment is not allowed in combination with the null conditional operator.
With methods is it not a problem at all though.
I always thought that properties are just syntactic sugar for setter methods.
item?.SetMarker(Marker.Start); // Perfectly fine
item?.Marker = Marker.Start; // Error CS0131 The left-hand side of an assignment must be a variable, property or indexer
So why is one allowed and the other not?
I'm pretty sure there is a good theoretical reason.
With the small hope of getting smarter I just try to know why :)
PS - I noticed the same behaviour in TypeScript.
In the second line, when item is null, it does not have any Marker, so you are not able to assign any value to it.
On the other hand, In the first line, when item is null, SetMarker method is not even called and in another word you do not try to assign anything.
This question already has answers here:
How can we check reference equality for a type that implements equality operator?
(2 answers)
Uniquely Identifying Reference Types in the Debugger
(4 answers)
Closed 5 years ago.
how do I know during debugging whether obj and obj2 reference the same object in the following code?
object obj = new object();
object obj2 = new object();
obj2 = obj;
In VS 2008 I could easily see that by looking in the watch window or locals window or on the value tooltip of the variable, because they all showed the object address, which was really useful (code sample is C++/CLI, but was tested in C# as well):
But VS 2015 does not. How do I get that info here?
After further searching and testing I found
Uniquely Identifying Reference Types in the Debugger
which gives a good workaround by creating "object IDs". However, in my opinion this is just a workaround and I would prefer if I could see the required info instantly.
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 7 years ago.
I have the following enigmatic declaration in C#:
Dictionary<string, DateTime?> badCameras = new Dictionary<string, DateTime?>();
This was written by a programmer no longer here & IS compiling! What ever does the '?' mean following DateTime object? Has it something to do with a struct? I've searched online & am finding nothing? BTW, this is .NET3.5.
Thanks
DateTime? is compiler sugar for Nullable<DateTime>. Effectively, nullable types allow for the use of null when dealing with value types that don't normally support null. See here for details.
The ? in that context is a shortcut for Nullable<T>. In your example, its equivalant to:
Dictionary<string, Nullable<DateTime>>
The main purpose is to allow value types to hold the null value.
This question already has answers here:
What's the difference between dynamic (C# 4) and var?
(14 answers)
Closed 7 years ago.
I'm not sure what is the exact different between both declarations.
When should I use var and when should I use dynamic.
Thanks a lot!
Don
var is type of variable decided by the compiler at compile time. Need to initialize at the time of declaration. All errors are caught on compile time.
dynamic is type of variable decided by the compiler at runtime time. No need to initialize at the time of declaration. All errors are caught at runtime.
dynamic variables can be used to create properties and return values from a function. var variables cannot be used for property or return values from a function. They can only be used as local variable in a function.
This question already has answers here:
Direct casting vs 'as' operator?
(16 answers)
Closed 9 years ago.
I just ran across another way to cast objects in C#. I have always used (CastType)variable. I just noticed some code using variable as CastType. The latter reminds me of VB.
Is there a difference between the two methods of casting other than syntax?
The first one will throw InvalidCastException if the types don't match (refer to the documentation - "Explicit conversions" section). The second one (the as operator) will produce null value instead.