1

I don't understand what the following lines mean, please explain them to me.

1.

DateTime? pInsertDate;

At this variable declaration, What does ? mean?

2.

using (TransactionScope scope = new TransactionScope())

At this object creation, What does using mean?

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
eiphyo
  • 39
  • 2
  • Take a look at this question regarding `using`: http://stackoverflow.com/questions/75401/uses-of-using-in-c – Hans Olsson Dec 14 '10 at 08:15
  • You are asking two separate questions that both already have been answered for many times here at Stackoverflow. – Steven Dec 14 '10 at 08:42

3 Answers3

6
  1. The ? suffix is syntactic sugar for using Nullable<T>. So your declaration is equivalent to:

    Nullable<DateTime> pInsertDate;
    

    See the MSDN documentation for nullable value types for more information. Basically a nullable value type value can represent any of the values of the non-nullable underlying type (DateTime in this case) as well as the special "null" value. This isn't a null reference, but it's usually used with the same sort of connotations - "no value". For example, a Person class might have a DateTime DateOfBirth property, but a DateTime? DateOfDeath property, which would be null if the person was still alive.

  2. A using statement is a way of automatically calling Dispose on the reference acquired in the first part of the statement at the end of the body. So your code is broadly equivalent to:

    TransactionScope scope = new TransactionScope();
    try
    {
        // Body of the using statement
    }
    finally
    {
        if (scope != null)
        {
            scope.Dispose();
        }
    }
    

    In this case of course we know thatscope won't be null because we're calling a constructor, but that's the general expansion. (You could have used a method call to obtain the transaction scope, for example - in which case it could have returned null, but the generated code won't throw a NullReferenceException.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon, I need to ask you something. Is there any way to make an object nullable? I know that the objects are reference types and will be null if not referenced but my question is, is it possible to write myCustomObject? _temp = new myCustomObject() and assign the object to null on ctor if a condition is met? – Pabuc Dec 14 '10 at 08:18
  • 1
    @Pabuc: I think you're asking whether a constructor can return null - and the answer is no. Write a static method instead which either returns a reference to a new object or null. – Jon Skeet Dec 14 '10 at 08:21
  • So you can never write myCustomObject? _temp = new myCustomObject(); :/ – Pabuc Dec 14 '10 at 08:23
  • @Pabus: Not if `MyCustomObject` is a reference type. What would that even mean? It's already a nullable type, just not a nullable *reference* type... you can already test whether the current value is null, etc. – Jon Skeet Dec 14 '10 at 08:24
  • (constructor returning null) well, *it can* - both for structs (`T?`), and classes - but only in evil cases ;p – Marc Gravell Dec 14 '10 at 08:33
1

this is syntax for Nullable Types

Arseny
  • 7,251
  • 4
  • 37
  • 52
0

SomeType? is syntactic sugar for Nullable<SomeType>. It can only be applied to value types (not reference types) and indicates that the variable will also be able to store a value equivalent to what null is for reference types. (Value types cannot be null, so the Nullable<T> type was added to allow for such cases. It's extremely useful in database binding code, where nullable columns are common.)

using (SomeType x = y) { ... } is syntactic sugar for:

SomeType x = y;
try {
    ...
} finally {
    if (x != null)
        ((IDisposable)x).Dispose();
}

This pattern is common when using objects whose classes implement IDisposable, as a way to automatically clean up such objects when they are about to fall out of use.

cdhowie
  • 158,093
  • 24
  • 286
  • 300