What does ?? operator means in C#?
It's the null-coalescing operator.
It returns the first argument unless it is null, in which case it returns the second.
x ?? y
is roughly equivalent to this (except that the first argument is only evaluated once):
if (x == null)
{
result = y;
}
else
{
result = x;
}
Or alternatively:
(x == null) ? y : x
It is useful for providing a default value for when a value can be null:
Color color = user.FavouriteColor ?? defaultColor;
COALESCE
When used in a LINQ to SQL query the ??
operator can be translated to a call to COALESCE. For example this LINQ query:
var query = dataContext.Table1.Select(x => x.Col1 ?? "default");
can result in this SQL query:
SELECT COALESCE([t0].[col1],@p0) AS [value]
FROM [dbo].[table1] AS [t0]
What does @ mean in C#
It means interpret the following string as literal. Meaning, the \
in the string will actually be a "\"
in the output, rather than having to put "\\"
to mean the literal character
What is the meaning of ~ in c#
~UnitOfWork()
is the declaration of the destructor of the class public class UnitOfWork
.
Here's an (IMO) an interesting part to note about the destructors:
The programmer has no control over when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object. Destructors are also called when the program exits.
What does question mark and dot operator ?. mean in C# 6.0?
It's the null conditional operator. It basically means:
"Evaluate the first operand; if that's null, stop, with a result of null. Otherwise, evaluate the second operand (as a member access of the first operand)."
In your example, the point is that if a
is null
, then a?.PropertyOfA
will evaluate to null
rather than throwing an exception - it will then compare that null
reference with foo
(using string's ==
overload), find they're not equal and execution will go into the body of the if
statement.
In other words, it's like this:
string bar = (a == null ? null : a.PropertyOfA);
if (bar != foo)
{
...
}
... except that a
is only evaluated once.
Note that this can change the type of the expression, too. For example, consider FileInfo.Length
. That's a property of type long
, but if you use it with the null conditional operator, you end up with an expression of type long?
:
FileInfo fi = ...; // fi could be null
long? length = fi?.Length; // If fi is null, length will be null
c# meaning of ! before a statement
!
is the NOT (negation) logical operator.
It transform a false to true and true to false.
Boolean logical operators (C# reference)
Meaning of operator ? after an object in C#
It is a shorthand for Nullable<T>
, i.e. Point?
is the same as Nullable<Point>
. This allows you to assign a null value for value types.
See the MSDN reference on Using Nullable Types.
NOTE, in this context it is not an operator, it is a shorthand syntax.
What does {} mean in C#'
It's something called DBNull.Value
as stated before you can use it:
if (myDataGridView.Rows[0].Cells[1].Value == DBNull.Value)
{
//some code
}
Related Topics
Read Big Txt File, Out of Memory Exception
How to Change Route to Username After Logged In
How to Use Font Awesome Icons in Project as an Icon of Imagebutton
How to Iterate Through a Datatable
Only Primitive Types or Enumeration Types Are Supported in This Context
Use Different Name for Serializing and Deserializing with JSON.Net
Closures Behaving Differently in for and Foreach Loops
How to Add Code Outside the Scope of Main When Using C# 9 Top Level Statements
How to Implement Permission Based Access Control with ASP.NET Core
How to Count the Number of Elements That Match a Condition with Linq
String.Format - How It Works and How to Implement Custom Formatstrings
Case-Insensitive Dictionary with String Key-Type in C#
How to Programmatically Limit My Program's CPU Usage to Below 70%
Where Is My System.Numerics Namespace
Use Local Images in Webbrowser Control