Create a nullable type in Class Designer
Certain value types do not always have (or need) a defined value. This is common practice in databases, where some fields might not be assigned any value. For example, you might assign a null value to a database field to signify that it has not yet been assigned a value.
A nullable type is a value type that you extend so that it takes the typical range of values for that type and also a null value. For example, a nullable of Int32
, also denoted as Nullable<Int32>, can be assigned any value from -2147483648 to 2147483647, or it can be assigned a null value. A Nullable<bool> can be assigned the values True
, False
, or null (no value at all).
Nullable types are instances of the Nullable<T> structure. Each instance of a nullable type has two public read-only properties, HasValue
and Value
:
HasValue
is of typebool
and indicates whether the variable contains a defined value.True
means that the variable contains a non-null value. You can test for a defined value by using a statement such asif (x.HasValue)
orif (y != null)
.Value
is of the same type as the underlying type. IfHasValue
isTrue
,Value
contains a meaningful value. IfHasValue
isFalse
, accessingValue
will throw an invalid operation exception.
By default, when you declare a variable as a nullable type, it has no defined value (HasValue
is False
), other than the default value of its underlying value type.
Class Designer displays a nullable type just as it displays its underlying type.
For more information about nullable types in C#, see Nullable Types. For more information about nullable types in Visual Basic, see Nullable Value Types.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in this article. You might be using a different edition of Visual Studio or different environment settings. For more information, see Personalize the IDE.
To add a nullable type by using the Class Designer
In the class diagram, expand an existing class or create a new class.
To add a class to the project, on the Class Diagram menu, click Add > Add Class.
To expand the class shape, on the Class Diagram menu, click Expand.
Select the class shape. On the Class Diagram menu, click Add > Field. A new field that has the default name Field will appear in the class shape and also in the Class Details window.
In the Name column of the Class Details window (or in the class shape itself), change the name of the new field to a valid and meaningful name.
In the Type column of the Class Details window, declare the type as a nullable type by specifying the following:
int?
(Visual C#)Nullable(Of Integer)
(Visual Basic)
To add a nullable type by using the Code Editor
Add a class to the project. Select the project node in Solution Explorer, and, on the Project menu, click Add Class.
In the .cs or .vb file for the new class, add one or more nullable types in the new class to the class declaration.
From Class View, drag the new class icon to the Class Designer design surface. A class shape appears in the class diagram.
Expand the details for the class shape and move the mouse pointer over the class members. The tooltip displays the declaration of each member.
Right-click the class shape and click Class Details. You can view or modify the new type's properties in the Class Details window.