Data Types C# Language
When it comes to programming in C#, one of the fundamental concepts you need to grasp is data types. Data ty
pes specify the type of data that a variable can hold. They play a crucial role in memory allocation, data storage, and the operations you can perform on the data. In this post, we’ll explore the most common data types in C# and provide examples to help you understand their usage.Integer Data Types:
- int
The int
data type represents 32-bit signed integers. It is one of the most commonly used data types for storing whole numbers.
int age = 25;
- long
The long
data type represents 64-bit signed integers and is used when you need to work with larger numbers.
long population = 8000000L;
Floating-Point Data Types:
- float
The float
data type represents single-precision floating-point numbers. It is used for numbers with a decimal point.
float temperature = 98.6f;
- double
The double
data type represents double-precision floating-point numbers, providing higher precision compared to float
.
double pi = 3.14159265359;
Character Data Type:
- char
The char
data type is used to store a single character or a Unicode character.
char grade = 'A';
Boolean Data Type:
- bool
The bool
data type represents a Boolean value, which can be either true
or false
.
bool isWorking = true;
String Data Type:
- string
The string
data type is used for storing sequences of characters, such as text.
string greeting = "Hello, World!";
Enumeration Data Type:
- enum
Enumerations are user-defined data types that consist of a set of named integral constants. They are useful for improving code readability.
enum DaysOfWeek
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
DaysOfWeek today = DaysOfWeek.Wednesday;
Custom Data Types:
In C#, you can create custom data types using classes and structures. These allow you to define your own data structures and encapsulate data and behavior.
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person person1 = new Person
{
Name = "John",
Age = 30
};
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.