Download Dot Net study material PDF

TitleDot Net study material
TagsCommand Line Interface C (Programming Language) Library (Computing) Microsoft Visual Studio C Sharp (Programming Language)
File Size1.2 MB
Total Pages267
Document Text Contents
Page 133

.NET Book Zero Charles Petzold

Version 1.1 Page 132

which is nothing more nor less than the name of the Date structure. The
Date structure certainly seems to have a ToString method, but at the
moment it‘s not doing anything very interesting.

Every class and every structure in the .NET Framework—including those

classes and structure you write yourself—has a ToString method. The
ubiquity of ToString is made possible through inheritance, which is one
of the primary characteristics of object-oriented programming. When one
class inherits (or derives) from another class, the new class acquires all
the non-private fields and methods of the class it inherits from, and it
can add its own fields and methods to the mix. The class that a new
class derives from is called the base class, and base is also a C#
keyword.

One of the primary differences between classes and structures involves

inheritance. Classes can inherit from other classes, but a structure
exists mostly in isolation. A structure cannot explicitly inherit from
anything else, and nothing can inherit from a structure.

All classes and structures derive from the grand matriarch of the .NET
Framework, System.Object. In C#, the keyword object is an alias for
System.Object.

Another important class in the System namespace is System.ValueType,
which inherits directly from System.Object. Although structures can‘t
explicitly inherit from anything else, all structures implicitly derive from
System.ValueType.

The techniques and implications of inheritance will become more evident
in the chapters ahead. For now, you should know that the ToString
method exists in all classes and structures because it‘s defined in the
System.Object class like so:

public virtual string ToString()
{
...
}

Actually, just so you won‘t think the default implementation of ToString
is a long sophisticated chunk of code, I wouldn‘t be surprised if it were

implemented like this:

public virtual string ToString()
{
return GetType().FullName;
}

GetType is another method defined by System.Object. It returns an object
of type Type, and FullName is a property of the Type class that returns
the namespace and name of the type.

At any rate, take note of the virtual keyword. This keyword means that
any class or structure can provide a custom-made ToString method that

Page 266

.NET Book Zero Charles Petzold

Version 1.1 Page 265

bool? nb = null;

Or you can set it to a non-null value:

bool? nb = true;

Or you can set it from a non-nullable bool:

bool? nb = IsEnabled;

You can test the object against null, and you can use it in expressions
with casting:

if (nb != null)
{
if ((bool)nb)
{
... // true case
}
else
{
... // false case
}
}

Notice that you can put a nullable bool in an if statement directly:

if (nb) // Won’t work!

A new operator has been added to C# to help work with nullable types.
This is the null coalescing operator, which consists of a pair of question
marks. Suppose ndt is a nullable DateTime object. You can assign an
object of type DateTime like this:

DateTime dt = ndt ?? new DateTime(2007, 1, 1);

If ndt is not null, then dt is set to ndt.Value. Otherwise, ndt is set to a
DateTime object of January 1, 2007.

Besides the Nullable generic structure, the System class also contains a
Nullable class. This is a static class that you can use to compare two
objects based on nullable types, and it also has a static method named

GetUnderlyingType that you can use in connection with reflection.

At first it was believed that nullable types could be implemented without
any changes to the Common Language Runtime. But that proved not to

be the case. Suppose you have a nullable int:

int? ni;

And then there‘s code that might set ni to a value or might set it to null,
and then ni is cast to an object of type object:

object obj = ni;

Or perhaps this casting happens when ni is passed to a method

bool IsNull = TestForNull(ni);

and the method is defined with an argument of type object:

Similer Documents