techmore.in

C# - Variables

In C#, variables are used to store data that can be manipulated and retrieved throughout a program. They are essential for holding values, performing operations, and managing program state. Here’s a comprehensive guide to variables in C#:

1. Declaring Variables

To declare a variable in C#, specify the data type followed by the variable name. Optionally, you can initialize the variable with a value.

Syntax:

csharp
dataType variableName; // Declaration dataType variableName = initialValue; // Declaration with initialization

Examples:

csharp
int age; // Declaration age = 25; // Initialization double price = 19.99; // Declaration with initialization

2. Data Types

Variables in C# are strongly typed, meaning each variable must be declared with a specific data type. The data type determines what kind of data the variable can hold.

Basic Data Types:

  • Integer Types:

    • int - 32-bit signed integer
    • long - 64-bit signed integer
    • short - 16-bit signed integer
    • byte - 8-bit unsigned integer
  • Floating-Point Types:

    • float - 32-bit floating-point
    • double - 64-bit floating-point
  • Character and String Types:

    • char - 16-bit Unicode character
    • string - Sequence of characters
  • Boolean Type:

    • bool - Represents true or false

Example:

csharp
int number = 42; double temperature = 98.6; char letter = 'A'; string name = "Alice"; bool isActive = true;

3. Variable Initialization

Variables can be initialized at the time of declaration or later in the code.

Declaration and Initialization:

csharp
int count = 0; string greeting = "Hello, World!";

Initialization Later:

csharp
int count; count = 10; // Initialization after declaration

4. Constants

Constants are variables whose values cannot be changed once they are assigned. They are declared using the const keyword.

Syntax:

csharp
const dataType constantName = value;

Example:

csharp
const double Pi = 3.14159; const int MaxUsers = 100;

5. Variable Scope

The scope of a variable determines where it can be accessed within the code. C# variables have different scopes based on where they are declared.

  • Local Variables: Declared inside methods, constructors, or blocks. Accessible only within those methods or blocks.

    csharp
    void SomeMethod() { int localVar = 10; // Local variable }
  • Instance Variables: Declared within a class but outside of methods. They are accessed by instances of the class.

    csharp
    class MyClass { public int instanceVar; // Instance variable }
  • Static Variables: Declared with the static keyword. They are shared among all instances of a class.

    csharp
    class MyClass { public static int staticVar; // Static variable }

6. Variable Naming Conventions

  • Camel Case: For local variables and parameters, start with a lowercase letter and use uppercase for subsequent words (e.g., totalAmount).
  • Pascal Case: For fields, properties, and constants, start with an uppercase letter and use uppercase for subsequent words (e.g., MaxValue).

7. Var Keyword

C# allows you to use the var keyword for implicit type inference, where the compiler determines the type of the variable based on the assigned value.

Syntax:

csharp
var variableName = initialValue; // Type is inferred from the value

Example:

csharp
var number = 10; // int var name = "Alice"; // string var price = 19.99; // double

8. Nullable Types

C# provides the ability to use nullable types for value types, which can represent all the values of its underlying type plus an additional null value.

Syntax:

csharp
int? nullableInt = null; // Nullable int

Summary

Variables in C# are fundamental to storing and manipulating data. They must be declared with a specific data type, and their scope determines where they can be accessed. C# also supports constants, implicit type inference with var, and nullable types to enhance flexibility. Understanding how to properly use and manage variables is crucial for effective programming in C#.