C# equivalent of 'undefined'

B

Bob

IN JScript, we can use the keyword undefined as follows:

if (node!=undefined)
{
//do something
}

What is the equivalent of the above in C#?

Thanks,
Bob
 
D

Dave

if (node != null)
{
// do something
}

It is the job of the developer to use a null value as meaning "undefined" or unitialized. There is no true "undefined" equivalent
in C#.

If you declare a reference type:

private string mystring;

It will be initialized as "null" within the scope of a class.

If you have a method:

private void MyMethod(string Variable)
{
}

A caller may pass a null value in as "Variable".

If you do not wish to accept null values I might suggest that you then throw an exception, depending on the context of the method:

throw new ArgumentNullException("Variable");

-- Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top