Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will using work on null?

Tags:

c#

idisposable

Will the following code work if resource doesn't implement IDisposable?

  T resource = new T();
  using (resource as IDisposable)
  {
     ...
  }
like image 876
Prankster Avatar asked Apr 01 '09 21:04

Prankster


People also ask

Does Count work on NULL?

COUNT(expression) returns the number of values in expression, which is a table column name or an expression that evaluates to a column of data. COUNT(expression) does not count NULL values. This query returns the number of non-NULL values in the Name column of Sample. Person.

Which operator can be used with NULL?

To handle NULLs correctly, SQL provides two special comparison operators: IS NULL and IS NOT NULL. They return only true or false and are the best practice for incorporating NULL values into your queries.

Does group by work on NULL values?

GROUP BY does treat all NULL values equally.

What happens when you join on NULL?

The result of a join of null with any other value is null. Because null values represent unknown or inapplicable values, Transact-SQL has no basis to match one unknown value to another. You can detect the presence of null values in a column from one of the tables being joined only by using an outer join.


1 Answers

Yes. A using statement checks whether it's being given null and avoids trying to call Dispose if so.

From section 8.13 of the C# 3 spec:

A using statement is translated into three parts: acquisition, usage, and disposal. Usage of the resource is implicitly enclosed in a try statement that includes a finally clause. This finally clause disposes of the resource. If a null resource is acquired, then no call to Dispose is made, and no exception is thrown.

like image 189
Jon Skeet Avatar answered Sep 24 '22 16:09

Jon Skeet