C# DataTypes
In C#, data types define the type and size of values that can be stored in variables. C# provides several built-in data types, each serving a specific purpose. Let's explore some of the commonly used data types in C# along with examples:
Numeric Types
- int: Represents signed integers.
Example: int age = 30; - double: Represents double-precision floating-point numbers.
Example: double pi = 3.14; - decimal: Represents decimal numbers with higher precision for financial and monetary calculations.
Example: decimal salary = 5000.50M;
Boolean Type
- bool: Represents a Boolean value that can be either true or false.
Example: bool isStudent = true;
Character Types
- char: Represents a single character. Enclosed in single quotes.
Example: char grade = 'A';
String Type
- string: Represents a sequence of characters. Enclosed in double quotes.
Example: string name = "John Smith";
Arrays
- int[]: Represents an array of integers.
Example: int[] numbers = { 1, 2, 3, 4, 5 };
Enumerations
- enum: Represents a set of named values.
Example:
enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
DaysOfWeek today = DaysOfWeek.Wednesday;
DateTime Type
- DateTime: Represents a date and time value.
Example: DateTime now = DateTime.Now;
Nullable Types
- int?: Represents a value type that can also be assigned a null value.
Example: int? score = null;
Conclusion
Above showing are just a few examples of the data types available in C#. Understanding and utilizing the appropriate data types is crucial for storing and manipulating data accurately in your programs. By choosing the right data type, you can optimize memory usage and ensure proper data representation and manipulation within your C# applications.
Related Topics
- How to CultureInfo in C#
- How do I solve System.InvalidCastException?
- Difference between readonly and const keyword
- DateTime Format In C#
- Difference Between Task and Thread
- Object reference not set to an instance of an object
- How to Convert Char to String in C#
- How to Convert Char Array to String in C#
- How to convert int to string in C#?
Related Links