Nullable class

J

John S

Hello,

Is it so that a class can't be Nullable?
E.g. following code dosn't compile.
Have I understand Nullable classes wrong or is the syntax wrong?

Cheers,

- John
public class MyWay

{

private string myString;

public string MyString

{

get { return myString; }

set { myString = value; }

}

}

class Program

{

private Nullable<MyWay> myWay = null;

public void CallMyWay()

{

if (myWay.HasValue)

{

Console.WriteLine(MyString);

}

else

{

Console.WriteLine("MyWay is null");

}

}

static void Main(string[] args)

{

new Program().CallMyWay();

}

}
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

Is it so that a class can't be Nullable?
E.g. following code dosn't compile.
Have I understand Nullable classes wrong or is the syntax wrong?

A class is ALWAYS nullable, it was the values types that were not
nullables, hence the added Nullable type requires that the parameter
to be a valued type.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

Is it so that a class can't be Nullable?
E.g. following code dosn't compile.
Have I understand Nullable classes wrong or is the syntax wrong?

A class is ALWAYS nullable, it was the values types that were not
nullables, hence the added Nullable type requires that the parameter
to be a valued type.
 

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