Illustrated C# 2012

Daniel Solis

Mentioned 2

A guide to the C# programming language covers such topics as syntax, classes and objects, exception handling, strings, arrays, delegates, threading, and generics.

More on Amazon.com

Mentioned in questions and answers.

So I have this bit of code, if I break point on the return statement the immediate window outputs the information below.

try
{
    await connection.OpenAsync();
    var obj = await cmd.ExecuteScalarAsync();
    return obj != null ? Int32.Parse(obj.ToString()) != 1 : false;
}
catch (Exception ex)
{
    Log.Error("An error has occurred checking a customer/product authorization.", ex);
    return false;
}
finally
{
    connection.Close();
}

Stored Procedure Here is the relevant parts of the stored procedure. @HasAuthTable and @IsAuthorized are of the type bit.

SELECT (CASE WHEN @HasAuthTable = 0 THEN 1 ELSE 0 END) | @IsAuthorized AS IsAuthorized

Immediate Window

obj
0

obj == null
false

obj != null
false

obj == 0
error CS0019: Operator '==' cannot be applied to operands of type 'object' and 'int'

obj != 0
error CS0019: Operator '!=' cannot be applied to operands of type 'object' and 'int'

(int)obj == 0
true

(int)obj != 0
false

obj.GetType().FullName
"System.Int32"

obj.Equals(null)
false

!obj.Equals(null)
true

Object.ReferenceEquals(obj, null)
false

!Object.ReferenceEquals(obj, null)
false

I've tried Rebuilding the solution didn't change anything. I have also tried restarting Visual Studio. No luck. Is this intended behavior? It seems like a bug.

Altered Stored Procedure I tried changing the output of the stored procedure to match the following to see if it affects it in any way. The result is basically the same. static type of object with the expected dynamic type, both having values but still returning false for obj == null and obj != null.

SELECT CAST(((CASE WHEN @HasAuthTable = 0 THEN 1 ELSE 0 END) | @IsAuthorized) AS BIT) AS IsAuthorized

Respective Immediate Window

obj
false

obj != null
false

obj == null
false

obj.GetType().FullName
"System.Boolean"

var obj = await cmd.ExecuteScalarAsync(); is doing Boxing.

enter image description here

Immediate Window displays incorrect value if 0 is boxed. It must be a bug in Immediate Window.

enter image description here

The picture is from Illustrated C# 2012 4th Edition by Daniel Solis.

I read somewhere that when you add using System; to your .cs file, the compiler (or maybe something else) adds all System related classes to your .cs file and so maybe it is better to not always add using System; and instead reference your DateTime with its full namespace like this: System.DateTime, for example, if it is easy enough to do and few things reference System it in your .cs file.

Is that true and if so, can that hurt performance or increase the size of the app? If so, I realize that using System; is easier to write and is a convenience and so therefore, you must weigh convenience with performance. It also might be the case that only adding a using System; might not make much difference but when many references are added, maybe it could? Thanks!

Both using directive and full namespace generate the same IL.

enter image description here

enter image description here

It might be very tiny bit of extra work for Language compiler, but you want to sacrifice for readability.

enter image description here

Overview of the compile-time and runtime processes diagram is from Illustrated C# 2012 by Daniel Solis