0% found this document useful (0 votes)
3 views8 pages

Variable

The document provides an overview of C# variables, including their types such as int, double, char, string, and bool, as well as the importance of unique identifiers for variables. It outlines variable naming conventions and the syntax for declaring variables, including the use of constants with the 'const' keyword. Additionally, it explains how to display variable values using the WriteLine() method and demonstrates how to declare multiple variables at once.

Uploaded by

sohanxt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Variable

The document provides an overview of C# variables, including their types such as int, double, char, string, and bool, as well as the importance of unique identifiers for variables. It outlines variable naming conventions and the syntax for declaring variables, including the use of constants with the 'const' keyword. Additionally, it explains how to display variable values using the WriteLine() method and demonstrates how to declare multiple variables at once.

Uploaded by

sohanxt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Variable

Course Code: CSE0613201


Lecturer: Sumaiya Tanjil Khan (STK)
C# Variables

➔ Variables are containers for storing data values.


➔ In C#, there are different types of variables (defined with different
keywords), for example:
● int - stores integers (whole numbers), without decimals, such as 123 or -123
● double - stores floating point numbers, with decimals, such as 19.99 or -
19.99
● char - stores single characters, such as 'a' or 'B'. Char values are surrounded
by single quotes
● string - stores text, such as "Hello World". String values are surrounded by
double quotes
● bool - stores values with two states: true or false
C# Identifiers

● All C# variables must be identified with unique names.


● These unique names are called identifiers.
● It is recommended to use descriptive names in order to create
understandable and maintainable code.
Variable naming conventions

● A variable can have alphabets, digits and underscore.


● A variable name can start with alphabet and underscore only.
● It can't start with digit.
● No white space is allowed within variable name.
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
Syntax:

type variableName = value;

Example:
int i, j;
double d;
float f;
char ch;
string s;

int myNum = 15;


Console.WriteLine(myNum);
C# Constants

If you don't want others (or yourself) to overwrite existing values, you
can add the const keyword in front of the variable type.

const int myNum = 40;

myNum = 75; // error


C# Display Variables

The WriteLine() method is often used to display variable values to the console
window.
Example:
string batch = "Batch2024";
Console.WriteLine("Hello " + batch);
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
Console.WriteLine(fullName);
int x = 5;
int y = 6;
Console.WriteLine(x + y);
Declare Many Variables

➔ int x = 5, y = 6, z = 50;

Console.WriteLine(x + y + z);

➔ int x, y, z;

x = y = z = 50;
Console.WriteLine(x + y + z);

You might also like