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

  1. int: Represents signed integers.
    Example: int age = 30;
  2. double: Represents double-precision floating-point numbers.
    Example: double pi = 3.14;
  3. decimal: Represents decimal numbers with higher precision for financial and monetary calculations.
    Example: decimal salary = 5000.50M;

Boolean Type

  1. bool: Represents a Boolean value that can be either true or false.
    Example: bool isStudent = true;

Character Types

  1. char: Represents a single character. Enclosed in single quotes.
    Example: char grade = 'A';

String Type

  1. string: Represents a sequence of characters. Enclosed in double quotes.
    Example: string name = "John Smith";

Arrays

  1. int[]: Represents an array of integers.
    Example: int[] numbers = { 1, 2, 3, 4, 5 };

Enumerations

  1. enum: Represents a set of named values.

Example:

enum DaysOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; DaysOfWeek today = DaysOfWeek.Wednesday;

DateTime Type

  1. DateTime: Represents a date and time value.
    Example: DateTime now = DateTime.Now;

Nullable Types

  1. 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.