C# Data Types with Best Examples(.NET v4.7)

In this tutorial, I will take you through the understanding of different C# Data Types. As C# is an object-oriented language, and at the basic level it contains building blocks known as classes. The classes interact with one another, and as a result, provide functionality at runtime. A class consists of two components:

Data attributes: Data attributes refer to the different properties defined in the class object.

Methods: Methods indicate the different operations that are to be executed in the class object.

C# Data Types with Best Examples(.NET v4.7) 1

Also Read: HTML Tables with Best Examples

C# Data Types

In C#, a variable can acquire one of the following types:

a)A value type
b)A reference type

C# differentiates between these two types in terms of how these values are saved and maintained in the Global Assembly Cache (GAC) during the program execution. Value type variables are saved in the stack, while reference type variables are saved in a managed heap data structure. Both are very important C# Data Types.

There are other pointer types that allow us to access value in the memory location of a variable. As we go more on Creating and Using C# data types, we will explore those C# data types in detail. For now, let’s look at these two C# data types and explore them in detail.

Value type variables

In value types, the variables contain the data or the contents of the variable. This implies that if any change is made to a value type variable in a different scope of the program, the change will not be reflected back once the control shifts to the calling function.The following are the different types of value types in C#.

Simple types

Following is the list of simple C# data types:

Int: For example 1, 2, 4, and -100. They can be both signed and unsigned. A signed int type can be both positive and negative. An unsigned int type cannot be negative; its minimum value is 0.

Float: For example, 3.14.

Long: Unlike Int, which is 32-bit, Long is a 64-bit integer value. It can also be both signed and unsigned.

Decimal: Like Float, decimal which is another C# data types also represent decimal digit numbers with the main difference being in terms of precision. For Float data members, the precision is 7; however, in the case of decimal data types, the precision is 28 digits.

Char: Represents a single character sequence. It can acquire values such as C, c, or white-space, any special characters – such as % and # – and even a digit such as 1.

bool: It can be used to represent variables that acquire a digital value such as true or false.

Enum types

Enum types are used to indicate an attribute that can acquire a constant set of values, for example, enum Day {Sat, Sun, Mon, Tues, Wed, Thurs, Fri}.

By default, the value of the first enumerator in the declaration starts from 0. It then increments the value of the subsequent enumerators by 1. For the preceding example, the following would be the value of the enumerators:

Sat – 0

Sun – 1

Mon – 2

Tues – 3

Wed – 4

Thurs – 5

Fri – 6

We can also override the default values of the enumerators by explicitly defining the values in the declaration itself. For example, in the preceding example, if we do not want the enumerators to start from 0, we can use the following declaration:

enum Day {Sat = 1, Sun, Mon, Tues, Wed, Thurs, Fri}

For the preceding declaration, the enumerators will acquire the following values:

Sat – 1

Sun – 2

Mon – 3

Tues – 4

Wed – 5

Thurs – 6

Fri – 7

Each Enumerator attribute also has an underlying data type that, by default, is of type Int. If required, we can also change the type of the enumerated values to long or short. However, it cannot take char as an underlying data type. Refer to the following enum declaration, in which we are setting the type of Enumerator value to short:

enum Day : short {Sat = 1, Sun, Mon, Tues, Wed, Thurs, Fri}

Struct types

Just like classes, structs in C# can be used to group together related data. Like classes, they can have constructors, fields, and methods. However, there are some differences between the implementation of structs and classes.

Reference type variables

In reference type variables, the data member contains the exact address of the variable in memory. As the variable just contains a reference to the memory address, two separate reference type variables can point to the same memory address. Therefore, if a change is made to a reference type variable, the change is directly done at the memory location of the variable. Due to the change being directly made at the memory location of the variable, both variables will reflect the updated value.

The following are the reference types available in C#:

Class: A class represents a collection of related properties and methods.

Interface: An interface in C# represents a collection of related properties, events, and methods, with just a declaration and no definition. In this chapter, in upcoming sections, we will deep dive into interfaces and see how they are implemented in C#.

Dynamic: A dynamic type variable avoids compile-time type checking. For example, if we declare a dynamics variable type and assign a variable to it, its type is defined at runtime when a value is assigned to it.

For example, in the following code snippet, we are creating a dynamics type variable, assigning different variables to it and evaluating its type at runtime:

dynamic typeVariable = 100;
Console.WriteLine(typeVariable + ” ” + typeVariable.GetType().ToString());// Output 100 System.Int32
typeVariable = “Hello”;
Console.WriteLine(typeVariable + ” ” + typeVariable.GetType().ToString());// Output Hello System.String
typeVariable = true;
Console.WriteLine(typeVariable + ” ” + typeVariable.GetType().ToString());// Output True System.Boolean Console.ReadLine();

Object: When a new instance of a class is created using the new keyword, an object for the class is created in the memory.

String: A String object is a sequence of Char objects whose value is immutable or read-only. This basically implies that, when we modify a variable of type String, it creates a new object in memory.

Reference: Programming in C#

Leave a Reply