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:

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 = "nesto", 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:

int number = null;

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:C#

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 HasValueproperty 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:

int ? number = null;
 number = 234; //comment this line to print out the result from the else block 
 if (number.HasValue) {
 } else {
     Console.WriteLine("number is null");

Data Types

Data types that represent the whole numbers are expressed with a certain number of bits. For unsigned numbers, the range is from 0 to 2N-1, and signed numbers range is from -2N-1 to 2N-1-1. So if the data type has a size of 8 bits like the sbyte data type, we can represent its range like this: from -27 to 27-1 => from -128 to 127.

The following table contains different data types that represent the whole numbers:

whole numbers table - C# Data Types

Letter u in front of the type means that type can’t contain negative numbers, it is unsigned.

The types mentioned above are the whole number types. But in C#, we have the number types with the floating point.

We can present them in a table as well:

Decimal numbers table in C# Data Types

In C#, we have two more basic data types:

Char-bool table C# Data Types

To use char type in our code we must place it inside the single quotes: ’a’ or ’A’ or ’3’…

One more type that is often introduced as the basic data type is the string type. But the string is not a value type it is a reference type. To use a string in our code we must place the value inside the double quotes: „This is the string type“ or „3452“…

So, we know we have the value types and reference types, and it is time to talk more about them and variables as well.

Variables in C#

Variable is a name of a memory location in which application stores values.

We should create our variables by following examples:

  • studentName
  • subject
  • work_day …

The wrong examples would be

  • student Name
  • work-day
  • 1place

We should mention that C# is a case-sensitive language so the studentName is not the same as the StudentName.

The C# language has its own set of reserved words, so-called keywords. We can’t use them as a name for our variables.

In C#, we have variables divided into two categories: Value type and Reference type. The difference is that the value type variables stores their values inside their own memory locations, but the memory location for the reference type variables contains only address to the dynamic memory location where the value is stored.

We should declare our variables in the following way:

<data type> <variable name> ;  or <data type> <variable name>, <variable name> ... ;

So a few examples would be:

class Program {
     static void Main(string[] args) {
         int age;
         double temperature,
         Student student;

Just with the declaration, we can’t assign a value to a value type variables. To do that, we need to use expressions in addition:

<data type> <variable name> = <expression> ;

Again, let’s look at this with an example:

class Program {
     static void Main(string[] args) {
         int x = 5;
         int y = 145 + x;
         char p = 'p';
         p = 'A';

To add a value for the reference type variable, we need to use the newkeyword in the expression part (string is an exception to this rule):

class Program {
     static void Main(string[] args) {
         Student student = new Student("John", 25);

We would like to mention that we don’t recommend to call variables with names „x“ or „y“… We have used that names just for the sake of simplicity. It is a better idea to give meaningful names to our variables.