In this article, we are going to talk about anonymous classes, how to create them, and why they are useful. Moreover, we are going to talk about nullable types and how to use them with the value types and what properties we have with the nullable types.

If you want to see complete navigation of this tutorial, you can do that here C# Intermediate Tutorial.

To download the source code, you can visit Anonymous and Nullable Types in C# Source Code. 

We are going to divide this article into the following sections:

Anonymous Classes

An anonymous class is a class that does not have a name. This sound strange but sometimes an anonymous class can be useful, especially when using query expressions.

Let’s see what we mean by that.

We can create an object of anonymous class simply by using the new keyword in front of curly braces:

This object contains two properties the Name and the Age. The compiler will implicitly assign the types to the properties based on the types of their values. So what this means basically is that the Name property will be of the string type and the Age property of the int type.

But now, we can ask, what type the myAnonymousObj is? And the answer is that we don’t know, which is the point of anonymous classes. But in C# this is not a problem, we can declare our object as an implicitly typed variable by using the var keyword:

The var keyword causes the compiler to create a variable of the same type as the expression that we use to initialize that object. So let’s see a couple of examples of well-known types:

We can access the properties of our anonymous object the same way we did with regular objects:

Nullable Types

The null value is useful for initializing reference types. So, it is logical that we can’t assign the null value to the value type because the null is itself a reference.

That being said, we can see that the following statement will throw an error:

nullable type error - Anonymous and Nullable types in C#

However, C# provides us with a modifier that we can use to declare a value type as a nullable value type. We can use the ? sign to indicate that value type is nullable:

We can still assign an integer value to our nullable value type:

This is all valid. But if we try to assign the variable of an int type with a value of our nullable type, we are going to have a problem:

This makes sense if we consider that the variable number might contain the null but the variable another can’t contain null at all.

Properties of Nullable Types

The nullable types expose a few properties which can come in handy while working on our projects. The HasValue property indicates whether a nullable type contains a value or it is a null. The Value property enables us to retrieve the value of the nullable type if it is not a null:

Conclusion

In this article, we have learned:

  • How to use anonymous classes
  • What the nullable types are
  • About properties of nullable types

In the next article, we are going to talk about Structures in C#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.