What Are Variables In C#.
Variables are specific names given to locations in the memory for storing and dealing with data. The values of a variable can be changed or reused as many times as possible. Every variable name has a specific type that resolves the size and layout of memory the variable will hold as well as the range of values that variable within your program can hold. Also, programmers can determine which variables can be applied to which type of operation.
Types Of C# Variables.
The basic types of variables that can be formed in a C# program are:
Integral
typessbyte, byte, short, ushort, int, uint, long, ulong, and char
Floating-point types
float and double
Decimal types
decimal
Boolean types
true or false values, as assigned
Nullable types
Nullable data types
Define Variables In C#.
For implementing variables in a C# program, you have to define them before use. To do this, the syntax is:
<data_type> <variable_names>;
Here data_types will be a valid data type (such as char, int, float, double, or any other user-defined data type) of C#, and the variable_names will be the set of variable names which are valid C# identifiers separated by commas.
Example
char s, chrr;
int a, b, c;
float pi, sal;
double aadharno;
Common Examples.
char ch = 'g';
int xy = 6, roll = 42;
byte b = 22;
double pi = 3.14159;
float salary = 20000.0f;
Accepting Values In Program.
There is a particular function of the Console class that provides the function Readline() to take input from the user for storing them in a variable, which is ultimately a named memory location.
Let us see how to use this function name with any variable to fetch (using the keyboard) and assign values to that variable:
Example
double sal;
sal = Convert.ToDouble(Console.ReadLine());
In the above code snippet, the first line will declare a variable as a double type. The second line will first execute the right side, which will wait for the user to input any number and will convert the input values to double using the Convert.ToDouble() and will finally assign that value to the 'sal' variable using the assignment operator (=).
Comments
Post a Comment