Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hi,

Please somebody help me on How to handle null value in C#.
Posted
Comments
Sergey Alexandrovich Kryukov 29-Dec-13 22:52pm    
Off-topic. Did you guess why?
—SA
Dave Kreskowiak 30-Dec-13 0:08am    
Do you expect us to read your mind? What you do mean, exactly, when you said "handle null value"??

Handling nulls is a very tricky topic that requires some of the same precautions used in handling highly radioactive materials. While domesticated nulls seem to be so much less fierce than their ancestors, the wild nulls, which interbred with bugs somewhere in the murk of prehistory, they still have that streak of unpredictably insectoid predatory behavior that can surprise even the most diligent programmer.

Nulls in the wild are, generally, more dangerous than domesticated nulls. Trapping the wild null is most often done using those retired three comedians "try," "catch," and "finally." [^], [^]

But, sometimes "using" is brought into play, even though it may provoke protests from topless Russian agitators who are convinced "using" is foul-play reeking of slavery and colonialism, not to mention abuse. [^]

Of course, many programmers use the common roadblock security check-point techniques of the different flavors of "IsNull," no matter the fact that their variables feel degraded by such intrusive body-cavity searches. [^]

For trapping the wild null on moonless nights, the nets you might use are the various wicked snares made by the TryParse corporation that are hung off tasty bait like "int," "Enum," and so forth. These methods require heartlessness and advanced training to deploy: a sacrificial variable victim must be passed into them, and will enjoy a horrible mutilation. [^]

Some programmers go so far as to surgically alter their variables to render them odorless so that nulls cannot locate them to attack, using the very controversial technique that turns them into "nullables," forcing them to wear, for a life-time, the Mark of the Question, "?," on their tails. [^]

The attacking null, if they can, by luck, grab onto a nullable and jump its bones, is somehow hypnotized by the iconic glyph (some believe it derives from the ancient Ankh of Egypt, brought by alien visitors to primordial Earth) into believing that, by mistake, they have attempted to mate with a reference Type, and simply drop-off using the same biological technique that lizards use to discard their tails.

In summary: for combat with nulls, wild and domesticated, the programmer must prepare diligently, and even then, with the most advanced training, and weapons, may fall prey to the subtle trickery of nulls since they delight in bringing the most complex software to its knees in the most undignified way possible.

Some say that at the heart of every null is the same savage ferocity that you see when a cat gets hold of a baby bird.

I am not sure about that.
 
Share this answer
 
v2
It totally depends on what you are going to do with this null value. First of all, don't try to dereference it, it won't give you anything good. Period.

Take a book on programming, CLR and C# and start reading it.

—SA
 
Share this answer
 
Comments
Ron Beyer 29-Dec-13 23:24pm    
+5, almost as good as "what do you do with an integer" or "how do you use a reference value".
Sergey Alexandrovich Kryukov 30-Dec-13 0:32am    
Thank you, Ron.
Exactly, "almost". About null is "better", because knowledge of plain English suggests: null is like "nothing". And what to do with it? Nothing.
—SA
C#
object value = null;

        if (value == null)
        {
            // means the object contains null values
        }
        else
        {
             // the object has some value..
        }
 
Share this answer
 
Hi..

C#
string sessionuserid = Session["username"] as string;

        if (string.IsNullOrEmpty(sessionuserid))
        {
            Response.Redirect("~/Admin/Login.aspx");
           
        }
 
Share this answer
 
XML
public static bool CheckNullOrEmpty<T>(T value)
{
     if (typeof(T) == typeof(string))
        return string.IsNullOrEmpty(value as string);

     return value == null || value.Equals(default(T));
}
How to use:

class Stub { }

bool f1 = CheckNullOrEmpty(""); //true
bool f2 = CheckNullOrEmpty<string>(null); //true
bool f3 = CheckNullOrEmpty(0); //true
bool f4 = CheckNullOrEmpty<Stub>(null);  //true
 
Share this answer
 
Comments
Dave Kreskowiak 30-Dec-13 0:08am    
So, all this does is check to see if there's it's null or not. The OP never said anything about what he meant by "handling" a null value, so this code is pretty much just a wild ass guess.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900