Variables and Data Types in C#

Variables and Data Types in C#

Variables

A variable is a named storage location in memory that holds a value. In C#, you must declare a variable before using it.

data_type variable_name;

Data Types

C# has a rich set of data types to represent different kinds of data.

1. Value Types

Value types directly store their values. When you assign a value type to a variable, the value itself is copied.

1.1. Primitive Data Types

  • int: Represents integer numbers (e.g., 42, -10)

    • Range: -2,147,483,648 to 2,147,483,647

    • int age = 23;

  • double: Represents floating-point numbers (e.g., 3.14, -2.5)

    • Range: Approximately ±5.0 × 10⁻³²⁴ to ±1.7 × 10³⁰⁸

    • Precision: 15-16 decimal digits

    • Usage: For general-purpose floating-point calculations

    • double pi = 3.14159;

  • float: Represents single-precision floating-point numbers

    • Range: Approximately ±1.5 × 10⁻⁴⁵ to ±3.4 × 10³⁸

    • Precision: 7-8 decimal digits

    • Usage: For less precise floating-point calculations, often used for performance reasons

    • float largeNumber = 3.4e38f;

  • decimal: Represents decimal numbers with high precision

    • Range: ±1.0 × 10⁻²⁸ to ±7.9 × 10²⁸

    • Precision: 28-29 significant digits

    • Usage: For financial calculations and other scenarios where precision is critical

    • decimal price = 19.99M;

  • bool: Represents boolean values (true or false)

    • bool isRaining= true;
  • char: Represents a single character (e.g., 'A', '!')

    • char initial = 'A'; char symbol = '$';

1.2. User Defined Types:

  • Struct: The Lightweight Value Type User-Defined Value Type

    • Similar to classes but stored on the stack, making them more efficient for small data structures.

    • Cannot be inherited from.

    • Example:

    • struct Point {

    • public int X { get; set; }

    • public int Y { get; set; }

    • }

    • | Feature | Value Type (Struct) | Reference Type (Class) | | --- | --- | --- | | Memory Allocation | Stack | Heap | | Assignment | Copies the entire value | Copies the reference | | Default Value | All fields are initialized to their default values (0 for numeric types, false for bool, null for reference types) | All fields are initialized to null | | Mutability | Can be mutable or immutable | Mutable | | Inheritance | Cannot inherit from other structs or classes | Can inherit from other classes and implement interfaces |

2. Reference Types:

  • Object: Base class for all other types (The Grandparent of All Types)

      • The ultimate ancestor of every class in C#.

        • Provides fundamental methods like ToString(), Equals(), and GetHashCode(). (You will have these methods in all objects)

        • You rarely declare an object directly

          • Object obj = new object();
  • String: The Versatile Text Handler

    • Represents a sequence of characters.

    • Immutable: Once created, its value cannot be changed.

    • String pooling: Identical strings often point to the same memory location for efficiency.

    • Example: string greeting = "Hello, world!"

  • Array: The Organized Collection

    • A fixed-size collection of elements, all of the same data type.

    • Can be single-dimensional or multidimensional.

    • Example:

    • int[] numbers = { 1, 2, 3, 4, 5 };

    • string[] names = { "Alice", "Bob", "Charlie" };

  • Class: The Blueprint of Objects

    • Defines the properties and methods of objects.

    • Encapsulates data and behavior.

    • Example:

    • public class Person

    • {

    • public string Name { get; set; }

    • public int Age { get; set; }

    • public void Greet()

    • { Console.WriteLine("Hello, my name is " + Name);

    • }

  • Interface: The Contract

    • Defines a contract that classes must adhere to.

    • Specifies methods that must be implemented by classes that implement the interface.

    • Example: interface IShape { double Area(); double Perimeter(); }

  • Delegate: The Function Pointer

    • Represents a reference to a method.

    • Used for event handling, asynchronous programming, and callback mechanisms.

    • Example:

    • delegate int Calculate(int x, int y);

    • int Add(int x, int y) => x + y;

  • Enum: The Enumerated Type

    • Defines a set of named constants.

    • Useful for representing a fixed set of values.

    • Example:

    • enum Days

      { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }