Overview:
In this article i would like to publish an article about an Usage of Null Keyword in C#.net.
Description:
The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types.
The following example demonstrates some behaviors of the null keyword:
Program:
Output:
About Nullable:
Nullable types are instances of the Nullable struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, a Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable can be assigned the values true false, or null. The ability to assign null to numeric and Boolean types is especially useful when you are dealing with databases and other data types that contain elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined.
Following example demonstrates about nullable types
Output:
In this article i would like to publish an article about an Usage of Null Keyword in C#.net.
Description:
The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types.
The following example demonstrates some behaviors of the null keyword:
Program:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace Null123
{
class Class3
{
class MyClass
{
public
void MyMethod() { }
}
static void Main(string[]
args)
{
// Set a
breakpoint here to see that mc = null.
// However,
the compiler considers it "unassigned."
// and
generates a compiler error if you try to
// use the
variable.
MyClass
mc;
// Now the
variable can be used, but...
mc = null;
// ... a
method call on a null object raises
// a run-time
NullReferenceException.
// Uncomment
the following line to see for yourself.
//
mc.MyMethod();
// Now mc has
a value.
mc = new
MyClass();
// You can
call its method.
mc.MyMethod();
// Set mc to
null again. The object it referenced
// is no
longer accsessible and can now be garbage-collected.
mc = null;
// A null
string is not the same as an empty string.
string
s = null;
string
t = String.Empty; //
Logically the same as ""
// Equals
applied to any null object returns false.
bool b
= (t.Equals(s));
Console.WriteLine(b);
// Equality
operator also returns false when one
// operand is
null.
Console.WriteLine("Empty string {0} null string", s == t ?
"equals": "does
not equal");
// Returns
true.
Console.WriteLine("null == null is {0}", null == null);
// A value
type cannot be null
// int i =
null; // Compiler error!
// Use a
nullable value type instead:
int? i
= null;
// Keep the
console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
About Nullable:
Nullable types are instances of the Nullable struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, a Nullable
Following example demonstrates about nullable types
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace Null123
{
class NullableExample
{
static
void Main()
{
int?
num = null;
// Is
the HasValue property true?
if
(num.HasValue)
{
System.Console.WriteLine("num
= " + num.Value);
}
else
{
System.Console.WriteLine("num
= Null");
}
// y
is set to zero
int
y = num.GetValueOrDefault();
//
num.Value throws an InvalidOperationException if num.HasValue is false
try
{
y = num.Value;
}
catch
(System.InvalidOperationException e)
{
System.Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}
No comments:
Post a Comment