C # equivalent of IsNull () function in SQL Server

In SQL Server, you can use the IsNull() function to check if a value is null, and if it is, return a different value. Now I am wondering if there is something similar in C #.

For example, I want to do something like:

 myNewValue = IsNull(myValue, new MyValue()); 

instead:

 if (myValue == null) myValue = new MyValue(); myNewValue = myValue; 

Thank.

+94
c # sql-server isnull
Oct 03 '08 at 22:59
source share
8 answers

He called the null coalescing ( ?? ) operator:

 myNewValue = myValue ?? new MyValue(); 
+175
Oct 03 '08 at 23:01
source share

Unfortunately, there is no equivalent to a null coalescing statement that works with DBNull; for this you need to use the ternary operator:

 newValue = (oldValue is DBNull) ? null : oldValue; 
+13
Oct 04 '08 at 0:20
source share

Use the Equals method:

 object value2 = null; Console.WriteLine(object.Equals(value2,null)); 
+5
Nov 14 2018-11-11T00:
source share
 public static T isNull<T>(this T v1, T defaultValue) { return v1 == null ? defaultValue : v1; } myValue.isNull(new MyValue()) 
+2
Jul 02 '15 at 15:34
source share

To work with DB Nulls, I created a bunch for my VB applications. I call them Cxxx2 because they are similar to the built-in functions of Cxxx VB.

You can see them in my CLR Extensions project.

http://www.codeplex.com/ClrExtensions/SourceControl/FileView.aspx?itemId=363867&changeSetId=17967

+1
04 Oct '08 at 4:50
source share

You write two functions

  //When Expression is Number public static double? isNull(double? Expression, double? Value) { if (Expression ==null) { return Value; } else { return Expression; } } //When Expression is string (Can not send Null value in string Expression public static string isEmpty(string Expression, string Value) { if (Expression == "") { return Value; } else { return Expression; } } 

They work very well

0
May 25 '18 at 19:20
source share

I used the following extension method for my DataRow types:

  public static string ColumnIsNull(this DataRow row, string colName, string defaultValue = "") { string val = defaultValue; if (row.Table.Columns.Contains(colName)) { if (row[colName] != DBNull.Value) { val = row[colName]?.ToString(); } } return val; } 

using:

 MyControl.Text = MyDataTable.Rows[0].ColumnIsNull("MyColumn"); MyOtherControl.Text = MyDataTable.Rows[0].ColumnIsNull("AnotherCol", "Doh! I'm null"); 

First, I check for the existence of a column, because if none of the query results has a nonzero value for this column, the DataTable will not even create this column.

0
Dec 20 '18 at 19:11
source share

This is meant half as a joke, since the question is stupid.

 public static bool IsNull (this System.Object o) { return (o == null); } 

This is an extension method, however it extends System.Object, so every object that you use now has an IsNull () method.

Then you can save tons of code by following these steps:

 if (foo.IsNull()) 

instead of super lame:

 if (foo == null) 
-12
04 Oct '08 at 5:14
source share