CHASH Tutorial



C# VARIABLES


Variables in C#

Variables are containers used to store data in a program. In C#, each variable must be declared with a specific data type that defines the kind of data it can hold.

Why Use Variables?

  • To store information like numbers, text, or other data types.
  • To manipulate and use data throughout your program.
  • To make your code flexible and easier to understand.

Declaring Variables

The basic syntax to declare a variable is:

data_type variableName;
  

Example:

int age;
string name;
double price;
  

Assigning Values

You can assign values to variables when declaring them or later in the code:

int age = 25;
string name = "Alice";
double price = 99.99;
  

Or assign values after declaration:

age = 30;
name = "Bob";
price = 49.95;
  

Common Data Types in C#

Data Type Description Example
int Stores whole numbers (integers) int score = 100;
double Stores decimal numbers (floating-point) double price = 12.99;
string Stores text string name = "John";
bool Stores true/false values bool isActive = true;

Tips for Using Variables

  • Choose meaningful names: Variables like age, price, or userName make your code easier to read.
  • Follow naming conventions: Use camelCase or PascalCase in C# (e.g., userName or UserName).
  • Initialize variables: Assign a value before using a variable to avoid errors.
  • Use the right data type: Picking the right type helps your program run efficiently.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review