In the previous tutorial, we created an example of mathematics operations in c#. We declared many variables and datatypes there. In this tutorial, we are going to learn about variables in C#. First of all, understand the variable.
A variable is the name of the memory location. In c programming, we made variables with data types. The variables take memory. Every variable name indicates the name of a memory location. A variable value can be changed during the execution time. Variables are used to a specific purpose to complete the process. In C# programming we can declare variables same as C programming. If you are already learned C programming then it will be easy for you. Variable can be reused.
Let's understand the variable types.
Variable type denotes the variable value which type of value stored in a variable and used. Mainly we know the variable type to data types.
There is basic variable type in C# programming which is similar to C programming.
Variable Type | Example |
---|---|
Decimal types | decimal |
Boolean types | True or false value, as assigned |
Integral types | int, char, byte, short, long |
Floating-point types | float and double |
Nullable types | Nullable data types |
If you are already leaned C and C++ programming then it is similar too. In C# programming, we create a variable with data type and variable name(identifier).
Let's understand with a syntax -
type variable_name;
type - The type denotes the data types. You can use any data type for a specific purpose.
variable_name - A variable identification name . Variable can be defines in one alphabet like a,b ,c .
Let's understand the variable in C# with an example -
First of declaring variable -
int a,b c,x,yz,abc ;
float f,a,b,x,y,z,abc;
double d,xyz,abc,a,b,c ;
char ch,ch1,ch2;
We can decalare variable like as -
int math, mul , var; etc
Data types - int, float, double, char
int- Integer data types.
float - Floating point number.
double - Double number
char - character
In the above approaches, we discussed the declaration of the variable in C# programming. Let's assign values with variables. We can say provide value to the variable. In C#, we provide value same as C programming.
Let's provide value to the variable .
int x= 2;
int b= 7;
float f=24.6; float c=15.7; //Floating point number
char ch ='A'; char ch1 ='D';
We can declared variable like as-
int _x;
int x1;
int x2;
We declared variables.
Let's create an example for all data types.
using System;
namespace ProgramApplication{
class Program{
static void Main(string[] args) {
int x=36;
int x1=4;
int z;
float f1=5;
float f2=54;
float f3;
char ch='Y';
char ch2='N';
f3=f1+f2;
z=x/x1+x-x1;
Console.WriteLine(z);
Console.WriteLine(f3);
Console.WriteLine(ch);
Console.WriteLine(ch2);
Console.ReadKey();
}
}
}
Output
41 59 Y N
In the above example, we declared variable with values and used some of logics to display the values.
The above example helps to understand the variable in C# programming.