-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- What is Variables in C#?
- What is Constant Variable in C#?
What is Variables in C#?
- A variable is a container for storing data values. A variable is assigned with a datatype. Variable is a name of memory location.
- Variable is a placeholder of the information which can be changed at runtime. Variables allows to Retrieve and Manipulate the stored information.
- To create a variable, user need to specify the datatype and assign it a value (optional): datatype variableName = value; Or datatype variableName;
- C# variables are case sensitive, hence myVar and MyVar are not same.
- Variable names can contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as the character ‘_’.
- The name of the variables should not be started with a digit.
- Variable names should not match with C# keywords/Reserved words like int, char, bool, catch, for, while etc.
- Variable must be unique in the scope of declaration and can not duplicate.
- User can also declare a variable without assigning the value, and assign the value later: int i; i = 30;
- In case User want to declare more than one variable of the same type, they need to use a comma-separated list: int a = 20, b = 30, c = 40;
- User can also assign the same value to multiple variables in one line: int a, b, c; a = b = c = 40;
- The value stored in a variable can be changed during program execution and latest value will be considered during execution. int i = 50; i = 30; Console.WriteLine(i);
- In C#, all the variables must be declared before they can be used; otherwise, it will show a compile-time error.
- It is a good programming practice to initialize variables properly, otherwise sometimes program may produce unexpected result.
What is Constant Variable in C#?
- In case user don't want others (or self) to overwrite existing values, they can add the const keyword in front of the variable type. This will declare the variable as "constant", which means unchangeable and read-only. Constants are immutable values which are known at compile time and do not change for the life of the program. const int cVar = 15; cVar = 20; // error
- The const keyword is useful when user want a variable to always store the same value, so that others won't mess up the code. An example that is often referred to as a constant, is PI (3.14...).
- const can be declared at the class level and inside the method.
- Multiple constants of the same type can be declared at the same time, for example:
- const int Months = 12, Weeks = 52, Days = 365;
int a, b, c, d;
a = b = c = c = d = 500;
b = 600;
b = 700;
const double d1 = 34.67;
const int Months = 12, Weeks = 52, Days = 365;
Console.WriteLine(Months);
Console.WriteLine(Weeks);
Console.WriteLine(Days);
Console.WriteLine(d1);
//int a = 150, b = 250, c = 350;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
int i = 30;
int I = 40;
int my_Var12 = 100; ;
Console.WriteLine(i);
Console.WriteLine(I);
int j = 30;
Console.WriteLine(j);
Console.WriteLine(my_Var12);
Console.ReadKey();