Updated March 18, 2023
Introduction to C# Data Types
As the name suggests, the data type is the type of data you are going to store in your variable. The data type is used to suggest the compiler or interpreter which kind of data it is going to process and how much memory will be required for that data.
For Ex: int is a data type that stores numeric values and requires four bytes.
As C# is a strongly typed language, it is necessary to declare the type of variable or constant before using it. Appropriate use of data types saves memory and improves the performance of the application.
Syntax:
datatype <variable_name> = value;
Examples of C# Data Types:
1. int intVal = 55; In this example: int is a datatype, intVal is a variable_name, 55 is a value.
2. char charVal = ‘A’;
3. string strVal = “Hello World!”;
4. float floatVal = 15.5f;
5. bool boolVal = true;
Top 3 C# Data Types
C# data types are divided into three categories:
1. Value Types
- Directly stores the value of a variable in memory.
- Accepts both signed and unsigned literals.
There are two types of value data types in C#:
- Predefined data types like int, char, bool, etc.
- User-defined data types like enum, struct, etc.
2. Reference Types
- They store the address of variable i.e. they contain the reference to a variable.
- If the data is changed by one variable, the other variable will automatically get the updated value.
There are two types of reference data types in C#:
- Predefined types like Object, String
- User-defined types like Class, Interface
3. Pointer Types
- They contain the memory address of the variable.
Symbols used in pointer:
- &(ampersand): Address operator, determines the address of a variable
- *(asterisk): Indirection operator, access the value of an address
Examples of Different Data Types
Following are some examples of different data types in C#,
Example #1: Some Value Types
using System;
public class ValueDataTypes
{
public static void Main()
{
//int - 32-bit signed integer type
int i = 55;
//char - 16-bit Unicode character
char ch = 'A';
//short - 16-bit signed integer type
short s = 56;
//long - 64-bit signed integer type
long l = 5564;
//uint - 32-bit unsigned integer type
uint ui = 100;
//ushort - 16-bit unsigned integer type
ushort us = 80;
//ulong - 64-bit unsigned integer type
ulong ul = 3625573;
//double - 64-bit double precision floating point type
double d = 6.358674532;
//float - 32-bit single-precision floating point type
//float needs 'f' or 'F' as suffix
float f = 2.7330645f;
//decimal - 128-bit precise decimal values with 28-29 significant digits
//decimal needs 'm' or 'M' as suffix
decimal dec = 339.5m;
Console.WriteLine("Integer: " + i);
Console.WriteLine("Char: " + ch);
Console.WriteLine("Short: " + s);
Console.WriteLine("Long: " + l);
Console.WriteLine("Unsinged integer: " + ui);
Console.WriteLine("Unsinged short: " + us);
Console.WriteLine("Unsinged long: " + ul);
Console.WriteLine("Double: " + d);
Console.WriteLine("Float: " + f);
Console.WriteLine("Decimal: " + dec);
}
}
Output:
Example #2: Bool, Enum, and Struct Data Type
The structure is a composite type used to store related data with different data types. Enum is used to assign names to integral constants.
using System;
public class BoolEnumStruct
{
//declaring enum
enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
//declaring structure
struct Student
{
public int Id;
public string FirstName;
public string LastName;
public Student(int id, string fname, string lname)
{
Id = id;
FirstName = fname;
LastName = lname;
}
}
public static void Main()
{
//boolean data type
bool flag = true;
if(flag)
{
Console.WriteLine("Bool value: "+flag);
Console.WriteLine();
}
//Accessing enum value for Friday
Console.WriteLine("Enumeration:");
Console.WriteLine(Days.Friday);
Console.WriteLine((int)Days.Friday);
Console.WriteLine();
//passing values to structure members using constructor
Student student = new Student(1, "Riya", "Sen");
Console.WriteLine("Structure Members:");
Console.WriteLine(student.Id);
Console.WriteLine(student.FirstName);
Console.WriteLine(student.LastName);
}
}
Output:
Example #3: Reference Data Types
using System;
public class StrObjDynamic
{
public static void Main()
{
string str = "C# ";
str += "Data Types";
Console.WriteLine("String: "+str);
Console.WriteLine();
//declaring object
object obj;
obj = 100;
Console.WriteLine("Object: "+obj);
//displaying type of object using GetType()
Console.WriteLine(obj.GetType());
Console.WriteLine();
//declaring dynamic variables
dynamic value1 = "Hello World!";
dynamic value2 = 5296;
dynamic value3 = 6.5;
//displaying actual type of dynamic variables using GetType()
Console.WriteLine("Dynamic:");
Console.WriteLine("Type of value1: {0}", value1.GetType().ToString());
Console.WriteLine("Type of value2: {0}", value2.GetType().ToString());
Console.WriteLine("Type of value3: {0}", value3.GetType().ToString());
}
}
Output:
Example #4: Interface
An interface can have properties, methods, events, and indexers as its members. It only contains the declaration of its members. The implementation of its members is provided by the class implementing it implicitly or explicitly.
using System;
interface Shape
{
void rectangle();
}
public class Area : Shape
{
//implementing interface method
public void rectangle()
{
Console.WriteLine("Area of rectangle is Length * Breadth");
}
public static void Main(String[] args)
{
Area area = new Area();
area.rectangle();
}
}
Output:
Example #5: Delegate
A delegate is an object that holds the reference to the method.
using System;
public class DelegateDemo
{
// Declaring delegate
public delegate void Sum(int a, int b);
public void SumVal(int a, int b)
{
Console.WriteLine(a +"+"+ b+ " = {0}", a + b);
}
public static void Main(String[] args)
{
DelegateDemo delegateDemo = new DelegateDemo();
// Creating object of delegate
Sum sum = new Sum(delegateDemo.SumVal);
//Passing values to the method using delegate object
sum(100, 100);
}
}
Output:
Conclusion
- The value type is stored in the stack.
- A reference type is stored in heap.
- When a value type is converted to a reference type it is called boxing(implicit conversion process).
- When a reference type is converted to a value type it is called unboxing(explicit conversion process).
Recommended Articles
This is a guide to C# Data Types. Here we discuss the types along with different examples of data types. You may also look at the following articles to learn more-