C# Tutorial
A variable is an identifierr that denotes a memory location to store a data value and each variable has a type that determines what value can be stored in the variable.
A variable may take the different values at different times during execution of the program.
Variable name should be declared before it is used in the program.
Variable name can be selected by programmer in a meaningful way.
mean is one of the variable in your program which represent the mean value.
Few variable names are,
length width height etc..
Variable name may consistes of alphabets, digits and underscore with subject to following conditions,
Must not begin with a digit
Upper case and lower case are distinct
Should not be C# keyword
Space is not allowed
Any number of characters (Any length)
The variable whose value do not change during the execution of a program.
This C# program is used to explain how to declare and initialize the variables and also using contants.
using System; // System is a namespace public class VariableTest { // Main method which starts the program execution. public static void Main() { // delare variable int num; // define variables int a = 10, b = 30, c = 15; num = a * b * c; const string result = "Muliplication of a, b and c is: "; Console.WriteLine(result + num); Console.ReadKey(); } }
Output:
D:\C#_Programs>csc VariableTest.cs D:\C#_Programs>VariableTest Muliplication of a, b and c is: 4500
If we change the C# program to update the constant variable as below, you will get the compilation error.
We cannot update the constant variable value during execution of a program and only initialized during variable declaration using const keyword.
using System; // System is a namespace public class VariableTest { // Main method which starts the program execution. public static void Main() { // delare variable int num; // define variables int a = 10, b = 30, c = 15; num = a * b * c; const string result = "Muliplication of a, b and c is: "; result = "Product of a, b and c is: "; Console.WriteLine(result + num); Console.ReadKey(); } }
Compiler error:
Severity Code Description Project File Line Suppression State Error CS0131 The left-hand side of an assignment must be a variable, property or indexer ConsoleApp2 D:\C#_Programs\VariableTest.cs 18 Active
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page