C# Data Types


Data types are classifications that specify which type of value a variable can hold. They determine the kind of operations that can be performed on the data, how much memory it occupies, and how the data is stored. C# has several built-in data types, which can be broadly categorized into two main groups: value types and reference types.

1. Primitive Types (Value Types)

Primitive types represent single values and are stored directly in memory.

TypeSizeDescriptionDefinitionExample
int4 bytes (32 bits)Signed integer type for whole numbers.Represents a 32-bit signed integer, used for storing whole numbers.int age = 30;
long8 bytes (64 bits)Signed integer type for larger whole numbers.Represents a 64-bit signed integer, suitable for larger numerical values.long distance = 10000000000L;
short2 bytes (16 bits)Signed integer type for smaller whole numbers.Represents a 16-bit signed integer, used for smaller whole numbers.short temperature = -5;
byte1 byte (8 bits)Unsigned integer type for small positive whole numbers.Represents an 8-bit unsigned integer, used for small positive whole numbers.byte level = 255;
sbyte1 byte (8 bits)Signed integer type for small numbers.Represents an 8-bit signed integer, used for small numbers, both positive and negative.sbyte signedValue = -100;
uint4 bytes (32 bits)Unsigned integer type for whole numbers without negatives.Represents a 32-bit unsigned integer, used for storing non-negative whole numbers.uint positiveNumber = 4000000000U;
ulong8 bytes (64 bits)Unsigned integer type for very large whole numbers.Represents a 64-bit unsigned integer, suitable for very large numerical values.ulong bigNumber = 1000000000000UL;
ushort2 bytes (16 bits)Unsigned integer type for small positive whole numbers.Represents a 16-bit unsigned integer, used for small positive whole numbers.ushort positiveShort = 60000;
float4 bytes (32 bits)Single-precision floating-point type for decimal values.Represents a 32-bit single-precision floating-point number, used for storing decimal values.float pi = 3.14f;
double8 bytes (64 bits)Double-precision floating-point type for more precise decimal values.Represents a 64-bit double-precision floating-point number, suitable for precise decimal calculations.double e = 2.718281828459;
decimal16 bytes (128 bits)High-precision type for financial and monetary calculations.Represents a 128-bit precise decimal value, used primarily for financial calculations to avoid rounding errors.decimal price = 19.99m;
char2 bytes (16 bits)Represents a single Unicode character.Represents a single 16-bit Unicode character, used for storing character data.char initial = 'A';
bool1 byteRepresents a truth value (true or false).Represents a Boolean value, indicating true or false conditions.bool isActive = true;

2. Non-Primitive Types (Reference Types)

Non-primitive types are more complex data types that can store collections of values or more complex data structures. They are stored as references in memory.

TypeDescriptionDefinitionExample
stringRepresents a sequence of characters (immutable).A sequence of characters used to represent text, immutable once created.string greeting = "Hello, World!";
arrayA collection of elements of the same type.A data structure that holds a fixed-size sequence of elements of a single type.int[] numbers = { 1, 2, 3, 4, 5 };
classA blueprint for creating objects, encapsulating data and behavior.A reference type that can contain data members (fields) and methods (functions), allowing for object-oriented programming.`class Person { public string Name;

3. Nullable Types

C# also supports nullable types, which allow value types to represent an undefined state. This is done using the ? syntax.

  • Example: int? can hold an integer value or null.

4. Enums

  • enum: A special "value type" that enables a variable to be a set of predefined constants.

5. Dynamic Type

  • dynamic: A type that bypasses compile-time type checking, allowing you to work with variables that can change types at runtime.

Examples :

1. Value Types

Integral Types

csharp
1int age = 30; // 32-bit signed integer 2long population = 7800000000L; // 64-bit signed integer 3short temperature = -5; // 16-bit signed integer 4byte score = 255; // 8-bit unsigned integer

Floating Point Types

csharp
1float height = 5.9f; // 32-bit single-precision floating-point 2double weight = 70.5; // 64-bit double-precision floating-point 3decimal price = 19.99m; // 128-bit precise decimal for financial calculations

Other Value Types

csharp
1char initial = 'A'; // A single 16-bit Unicode character 2bool isActive = true; // Boolean value

2. Reference Types

String

csharp
1string name = "John Doe"; // A sequence of characters

Arrays

csharp
1int[] numbers = { 1, 2, 3, 4, 5 }; // An array of integers

Class

csharp
1public class Person 2{ 3 public string Name { get; set; } 4 public int Age { get; set; } 5} 6 7Person person = new Person(); // Creating an instance of the Person class 8person.Name = "Alice"; 9person.Age = 28;

Object

csharp
1object obj = "Hello, World!"; // Object can hold any data type

3. Nullable Types

csharp
1int? nullableInt = null; // Nullable integer, can hold an integer or null

4. Enums

csharp
1public enum Days 2{ 3 Sunday, 4 Monday, 5 Tuesday, 6 Wednesday, 7 Thursday, 8 Friday, 9 Saturday 10} 11 12Days today = Days.Wednesday; // Using the enum

5. Dynamic Type

csharp
1dynamic dynamicVar = 10; // Initially an integer 2dynamicVar = "Now I'm a string"; // Can change to a string at runtime

Full Example

Here’s a complete example that incorporates several of these data types:

csharp
1using System; 2 3class Program 4{ 5 public enum Days 6 { 7 Sunday, 8 Monday, 9 Tuesday, 10 Wednesday, 11 Thursday, 12 Friday, 13 Saturday 14 } 15 16 static void Main() 17 { 18 // Value Types 19 int age = 30; 20 float height = 5.9f; 21 bool isStudent = false; 22 23 // Reference Types 24 string name = "Alice"; 25 int[] scores = { 85, 90, 78 }; 26 27 // Nullable Type 28 int? optionalValue = null; 29 30 // Enum 31 Days today = Days.Wednesday; 32 33 // Dynamic Type 34 dynamic dynamicVar = 100; 35 Console.WriteLine(dynamicVar); // Outputs: 100 36 dynamicVar = "Changed to a string"; 37 Console.WriteLine(dynamicVar); // Outputs: Changed to a string 38 39 // Output 40 Console.WriteLine($"Name: {name}, Age: {age}, Height: {height}, Is Student: {isStudent}"); 41 Console.WriteLine($"Scores: {string.Join(", ", scores)}"); 42 Console.WriteLine($"Optional Value: {optionalValue}"); 43 Console.WriteLine($"Today is: {today}"); 44 } 45}

Explanation

  • The program defines an enum for days of the week.
  • It demonstrates the use of various value types (like intfloat, and bool), reference types (like string and arrays), nullable types, and dynamic types.
  • It prints out the values of these variables to the console.
Data types are classifications that specify which type of value a variable can hold. They determine the kind of operations that can be performed on the data, how much memory it occupies, and how the data is stored. C# has several built-in data types, which can be broadly categorized into two main groups: value types and reference types.

1. Primitive Types (Value Types)

Primitive types represent single values and are stored directly in memory.

TypeSizeDescriptionDefinitionExample
int4 bytes (32 bits)Signed integer type for whole numbers.Represents a 32-bit signed integer, used for storing whole numbers.int age = 30;
long8 bytes (64 bits)Signed integer type for larger whole numbers.Represents a 64-bit signed integer, suitable for larger numerical values.long distance = 10000000000L;
short2 bytes (16 bits)Signed integer type for smaller whole numbers.Represents a 16-bit signed integer, used for smaller whole numbers.short temperature = -5;
byte1 byte (8 bits)Unsigned integer type for small positive whole numbers.Represents an 8-bit unsigned integer, used for small positive whole numbers.byte level = 255;
sbyte1 byte (8 bits)Signed integer type for small numbers.Represents an 8-bit signed integer, used for small numbers, both positive and negative.sbyte signedValue = -100;
uint4 bytes (32 bits)Unsigned integer type for whole numbers without negatives.Represents a 32-bit unsigned integer, used for storing non-negative whole numbers.uint positiveNumber = 4000000000U;
ulong8 bytes (64 bits)Unsigned integer type for very large whole numbers.Represents a 64-bit unsigned integer, suitable for very large numerical values.ulong bigNumber = 1000000000000UL;
ushort2 bytes (16 bits)Unsigned integer type for small positive whole numbers.Represents a 16-bit unsigned integer, used for small positive whole numbers.ushort positiveShort = 60000;
float4 bytes (32 bits)Single-precision floating-point type for decimal values.Represents a 32-bit single-precision floating-point number, used for storing decimal values.float pi = 3.14f;
double8 bytes (64 bits)Double-precision floating-point type for more precise decimal values.Represents a 64-bit double-precision floating-point number, suitable for precise decimal calculations.double e = 2.718281828459;
decimal16 bytes (128 bits)High-precision type for financial and monetary calculations.Represents a 128-bit precise decimal value, used primarily for financial calculations to avoid rounding errors.decimal price = 19.99m;
char2 bytes (16 bits)Represents a single Unicode character.Represents a single 16-bit Unicode character, used for storing character data.char initial = 'A';
bool1 byteRepresents a truth value (true or false).Represents a Boolean value, indicating true or false conditions.bool isActive = true;

2. Non-Primitive Types (Reference Types)

Non-primitive types are more complex data types that can store collections of values or more complex data structures. They are stored as references in memory.

TypeDescriptionDefinitionExample
stringRepresents a sequence of characters (immutable).A sequence of characters used to represent text, immutable once created.string greeting = "Hello, World!";
arrayA collection of elements of the same type.A data structure that holds a fixed-size sequence of elements of a single type.int[] numbers = { 1, 2, 3, 4, 5 };
classA blueprint for creating objects, encapsulating data and behavior.A reference type that can contain data members (fields) and methods (functions), allowing for object-oriented programming.`class Person { public string Name;

3. Nullable Types

C# also supports nullable types, which allow value types to represent an undefined state. This is done using the ? syntax.

  • Example: int? can hold an integer value or null.

4. Enums

  • enum: A special "value type" that enables a variable to be a set of predefined constants.

5. Dynamic Type

  • dynamic: A type that bypasses compile-time type checking, allowing you to work with variables that can change types at runtime.

Examples :

1. Value Types

Integral Types

csharp
1int age = 30; // 32-bit signed integer 2long population = 7800000000L; // 64-bit signed integer 3short temperature = -5; // 16-bit signed integer 4byte score = 255; // 8-bit unsigned integer

Floating Point Types

csharp
1float height = 5.9f; // 32-bit single-precision floating-point 2double weight = 70.5; // 64-bit double-precision floating-point 3decimal price = 19.99m; // 128-bit precise decimal for financial calculations

Other Value Types

csharp
1char initial = 'A'; // A single 16-bit Unicode character 2bool isActive = true; // Boolean value

2. Reference Types

String

csharp
1string name = "John Doe"; // A sequence of characters

Arrays

csharp
1int[] numbers = { 1, 2, 3, 4, 5 }; // An array of integers

Class

csharp
1public class Person 2{ 3 public string Name { get; set; } 4 public int Age { get; set; } 5} 6 7Person person = new Person(); // Creating an instance of the Person class 8person.Name = "Alice"; 9person.Age = 28;

Object

csharp
1object obj = "Hello, World!"; // Object can hold any data type

3. Nullable Types

csharp
1int? nullableInt = null; // Nullable integer, can hold an integer or null

4. Enums

csharp
1public enum Days 2{ 3 Sunday, 4 Monday, 5 Tuesday, 6 Wednesday, 7 Thursday, 8 Friday, 9 Saturday 10} 11 12Days today = Days.Wednesday; // Using the enum

5. Dynamic Type

csharp
1dynamic dynamicVar = 10; // Initially an integer 2dynamicVar = "Now I'm a string"; // Can change to a string at runtime

Full Example

Here’s a complete example that incorporates several of these data types:

csharp
1using System; 2 3class Program 4{ 5 public enum Days 6 { 7 Sunday, 8 Monday, 9 Tuesday, 10 Wednesday, 11 Thursday, 12 Friday, 13 Saturday 14 } 15 16 static void Main() 17 { 18 // Value Types 19 int age = 30; 20 float height = 5.9f; 21 bool isStudent = false; 22 23 // Reference Types 24 string name = "Alice"; 25 int[] scores = { 85, 90, 78 }; 26 27 // Nullable Type 28 int? optionalValue = null; 29 30 // Enum 31 Days today = Days.Wednesday; 32 33 // Dynamic Type 34 dynamic dynamicVar = 100; 35 Console.WriteLine(dynamicVar); // Outputs: 100 36 dynamicVar = "Changed to a string"; 37 Console.WriteLine(dynamicVar); // Outputs: Changed to a string 38 39 // Output 40 Console.WriteLine($"Name: {name}, Age: {age}, Height: {height}, Is Student: {isStudent}"); 41 Console.WriteLine($"Scores: {string.Join(", ", scores)}"); 42 Console.WriteLine($"Optional Value: {optionalValue}"); 43 Console.WriteLine($"Today is: {today}"); 44 } 45}

Explanation

  • The program defines an enum for days of the week.
  • It demonstrates the use of various value types (like intfloat, and bool), reference types (like string and arrays), nullable types, and dynamic types.
  • It prints out the values of these variables to the console.

Talk to us?

Post your blog