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 sounds 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 the anonymous class simply by using the new
keyword in front of curly braces:
myAnonymousObj = new { Name = "John", Age = 32 };
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:
var myAnonymousObj = new { Name = "John", Age = 32 };
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:
var number = 15; // the number is of type int var word = "example"; //the word is of type string var money = 987.32; //the money is of type double
We can access the properties of our anonymous object the same way we did with regular objects:
Console.WriteLine($"The name of myAnonymousObject is {myAnonymousObj.Name}, the age is {myAnonymousObj.Age}");
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:
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:
int? number = null;
We can still assign an integer value to our nullable value type:
int? number = null; int another = 200; number = 345; number = another;
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:
int? number = null; int another = 200; another = number; //this is the 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 null:
int? number = null; number = 234; //comment this line to print out the result from the else block if(number.HasValue) { Console.WriteLine(number.Value); } else { Console.WriteLine("number is 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#.
Is it important to learn angular for asp.net core ?
No. Those are two different concepts. ASP.NET Core is a pretty broad area. So you can have an ASP.NET Core full stack app without any .js framework. On the other hand, if you build ASP.NET Core Web API, then you need some sort of client app to consume that API, but it can be Angular, React, Vue, whatever you want.