C# Data Types

Learn via video courses
Topics Covered

Overview

A data type is a classification or category of data that informs the compiler or interpreter about the nature of the data and how it should be processed. In C#, data types are classifications of data that provide information to the compiler about how the data should be stored in memory and what operations can be performed on it. Data types in C# define the size, format, and range of values that can be assigned to variables of those types.

What is C# Data Types?

C# is a strongly typed language, which means that variables must be explicitly declared with their data types. When declaring a variable in C#, you need to specify the variable's data type, indicating the kind of values it can store. Here's an example that demonstrates variable declaration with data types in C#:

Categories of Data Types in C#

Value Data Types

Value types in C# are data types that store the actual data in the variable. This means that the actual data is copied when a value type is assigned to another variable. For example, the following code assigns the value 10 to the variable x of type int:

  • Variables of value types directly contain the data of the assigned value.
  • When a value type variable is assigned to another variable, passed as an argument to a method, or returned from a method, the value itself is copied.
  • Value types include primitive types such as int, float, bool, char, and struct types.

Value types in C# can belong to either category and are not limited to predefined data types. Both predefined and user-defined data types can be value types in C#. Here's a breakdown of the two categories:

Predefined (Built-in) Value Data Types

Predefined data types are data types that are defined by the programming language. In C#, there are several predefined data types, including:

  • Integer: A whole number, positive or negative, without decimals. The most common integer types are int and long.
  • Floating point: A number with a fractional part, containing one or more decimals. The most common floating point types are float and double.
  • Character: A single Unicode character, represented as a 16-bit integer.
  • String: A sequence of characters, enclosed in double quotes.
  • Boolean: A value that can be either true or false.
  • Date and time: Represents a specific point in time, as a combination of year, month, day, hour, minute, and second. Here is a table of the predefined data types in C#, along with their size, range, and default value:
TypeSizeRangeDefault Value
bool1 bittrue or falseFALSE
byte8 bits0 to 2550
char16 bitsU+0000 to U+ffff\0'
decimal128 bits(-7.9 x 1028 to 7.9 x 1028) / 100 to 280.0M
double64 bits(+/-)5.0 x 10-324 to (+/-)1.7 x 103080.0D
float32 bits-3.4 x 1038 to + 3.4 x 10380.0F
int32 bits-2,147,483,648 to 2,147,483,6470
long64 bits-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
sbyte8 bits-128 to 1270
short16 bits-32,768 to 32,7670
uint32 bits0 to 4,294,967,2950
ulong64 bits0 to 18,446,744,073,709,551,6150
ushort16 bits0 to 65,5350

Example:

Output

User-defined value Data Types

User-defined data types in C# include structures (structs), enumerations (enums), classes, and other custom types created by the user.

Structure Type:

  • A structure type, often referred to as a struct, is a user-defined value type that encapsulates related data fields and related functionality into a single entity.
  • Structs are used to create lightweight data structures that don't require inheritance or reference semantics.
  • Structs are value types, meaning that when a struct is assigned to a new variable, a copy of the struct's value is made.
  • Structs can have fields, properties, methods, constructors, and other members, similar to classes.

Ouput

Enumeration Type:

  • An enumeration type, often referred to as an enum, is a value type that represents a set of named constants, typically used to define choices or options.
  • Enums provide a way to define a finite set of values that a variable can take, improving code readability and maintainability.
  • Each named constant in an enum is associated with an underlying integral value, typically starting from zero, which can be explicitly assigned.
  • Enums can be used in switch statements, comparisons, and other operations.

Output:

Reference Data Types

Reference types in C# do not directly contain the actual data but instead contain a reference to the memory location where the data is stored. They refer to objects that are allocated on the heap. Changes made to one variable that holds a reference type will be reflected in other variables that also reference the same object in memory.

Predefined (Built-in) Reference Data Types

Predefined data types are data types that are defined by the programming language. In C#, there are several Reference Data Types, including:

  • Object Type The object type is the ultimate base class for all other types in C#. This means that every type in C#, whether it is a value type or a reference type, ultimately inherits from the object type. This enables polymorphism and treating different types as a common base type. Changes made to the data stored in an object-type variable will be reflected in other variables referencing the same data. This reference semantics applies to all reference types, facilitating code reusability and flexibility.

Ouput:

  • Dynamic Type In C#, the dynamic type allows variables to hold values of any type with runtime type checking. It offers flexibility and late binding, as type checking occurs at runtime instead of compile time. Changes made to a dynamic type variable are reflected in other variables referencing the same data, leveraging reference semantics. The dynamic type enables adaptable code, working with different types dynamically. With its runtime type checking and reference-based behavior, dynamic typing in C# provides versatility for handling various data scenarios.

Output:

  • String Type The string type in C# represents text as a sequence of characters. While string variables are reference types, they possess unique characteristics. Strings are immutable, meaning their values cannot be changed once assigned. String operations typically create new string objects instead of modifying existing ones due to immutability. This behavior ensures the integrity of string values. The string type is widely used for storing and manipulating text-based data, offering convenience and efficiency in handling textual information in C# programs.

Ouput

Userdefined Reference Data Types

User-defined reference data types in C# are custom types created by the user or developer. These types allow for the creation of specialized data structures, abstractions, and reusable components tailored to specific needs. Here are some common examples of user-defined reference data types:

  • Classes In C#, the class data type is a user-defined reference type that serves as a blueprint or template for creating objects. It encapsulates data fields, properties, methods, events, and other members into a single entity. Here's an example of a simple class in C# called "Car":

Output

  • Interfaces Interfaces in C# establish a contract for classes to adhere to. They outline member signatures such as methods, properties, and events that implementing classes must provide. Interfaces promote polymorphism, allowing objects of diverse classes to be treated uniformly through a shared interface type. This offers a way to achieve a form of multiple inheritance by implementing multiple interfaces, enabling code reusability, and creating a common contract for interacting with different classes.

Pointer Data Type

Pointer data types in C# are a type of data that stores a reference to the memory location of another variable. This means that when you assign a value to a pointer variable, you are not assigning the value itself, but rather a reference to the value.

Although C# does support pointers, their usage is restricted to specific scenarios and is generally considered unsafe. The unsafe keyword is used to denote a block of code where pointers can be used. Pointers are typically used in interoperation scenarios with unmanaged code, such as calling functions in native libraries or dealing with memory-mapped files. Here's an example of how pointers can be used in C# within an unsafe block:

Ouput

Conclusion

  • C# is a strongly-typed language, which requires explicit declaration of variable data types.
  • C# supports various data types, including value types, reference types, and pointer types.
    • Value types store the actual data directly in the variable and include predefined (built-in) types like int, float, bool, and char, as well as user-defined struct types.
    • Reference types store references to the memory location of the data and include predefined types like object, dynamic, and string, as well as user-defined class and interface types.
    • Pointer types are a rarely used feature in C# and are typically limited to specific scenarios involving interoperation with unmanaged code.