C# variables are the most useful part of an object-oriented programming language. There are different types of variables in c-sharp and you need to declare the required type.
In this tutorial, you will learn different types of variables with examples. You will also learn how to create a constant variable in C#. So, let’s start the tutorial.
What are Variables in C-Sharp (C#)?
Variables are the memory locations that stores data of certain data type. You have to define a variable type in C# and give it a name to use for manipulation. The name should be unique and can help you easily identify them from other variables.
To use a variable in C#, you have to first declare its type. After that, you can use them in required operations.
Different Types of Variables in C-Sharp (C#)?
Types | Example | Description |
---|---|---|
int |
2, -2, 32, -32, etc. | Declare integer or number variable to store numeric values without decimals. |
double |
2.3, -2.3, 32.4, -32.4, etc. | Create a float variable to store numeric values with decimals. |
char |
‘a’, ‘A’, ‘b’, ‘B’, etc. | Define character variables to store single characters. The character should be placed within single quotes(‘ ‘). |
string |
“Ram”, “John”, “This is a string.”, etc. | Used to declare text variable to store text content. The text should be placed within the double quotes (” “). |
bool |
true or false | Used to declare boolean variable to store boolean values either true or false. |
Declare or Create Variables in C-Sharp (C#)
To declare a variable in C#, you have to use the syntax given below. You have to first add a data type like int
, char
, string
, etc. After that, give it a unique name to easily identify it from others. You can initially assign a value at the time of declaring or later assign a value to the variable.
The description of the above syntax is given below:-
Name | Description |
---|---|
data_type | It is the type of data like int , double , char , etc. |
variable_name | User-defined name for the variable which is unique and can be easily identified. |
value | It is the value assigned to the variable according to its type. |
Let’s see a simple example to declare a number variable to store numeric values. You have to use the data type num
to define the number variable as given below.
Example
1 |
int myNum = 9; |
Rules to Name a Variable in C#
You have to follow the rules given below to properly name your variable in C#:-
- The name should start with the alphabet.
- It should start with a lowercase letter.
- A variable can contain an alphabet (capital and small), digits, and underscore (_).
- White spaces are not allowed in variable names.
Display Variables in C-Sharp (C#)
To display a variable value in the output, you have to use the Console.WriteLine(myVarName)
in C#. Replace the myVarName
with your name of the variable in C#.
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { int myNum = 9; Console.WriteLine(myNum); } } } |
Output
Integer Variable in C-Sharp (C#)
To declare a variable in C#, you have to use the data type int
before the variable name. the below example shows the declaration of the numeric variable with a value assigned to it.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { int myNum = 12; Console.WriteLine(myNum); } } } |
Output
The above output prints the value assigned to the numeric variable in C#.
Float Variable in C-Sharp (C#)
A float variable is a variable that stores a value with a decimal point. To create a float variable in C#, you have to use the data type double
before the variable name.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { double myFl = 9.8; Console.WriteLine(myFl); } } } |
Output
The above result shows that the assigned value is with some decimal points.
Character Variable in C-Sharp (C#)
If you want to create a character variable, you have to use the data type char
before the variable name in C#. It stores a single alphabet that can be capitalized or in small letters.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { char myChar = 'a'; Console.WriteLine(myChar); } } } |
Output
The above output shows that the assigned value is a single character.
String Variable in C-Sharp (C#)
To create a string variable in C#, you have to use the data type string
before the variable name. You can place any size of text content to the variable to declare a string.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { string myStr = "This is a string."; Console.WriteLine(myStr); } } } |
Output
The output given above shows that the assigned value to the variable is text content.
Boolean Variable in C-Sharp (C#)
A boolean variable is a variable that store boolean values true or false. To create a boolean variable in C#, you have to use the data type bool
before the variable name.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { bool myBl = true; Console.WriteLine(myBl); } } } |
Output
The above output shows the boolean value true.
What is a Constant Variable and How to Create a Constant Variable in C#
A constant variable in C# is a variable whose value cannot be changed and remains the same once assigned. To make a constant variable, you have to use the keyword const
before the data type. However, you cannot create a constant variable without assigning a value to it.
The below example shows the declaration of a constant variable in C#.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { const int myNum = 24; Console.WriteLine(myNum); } } } |
Output
Assign Multiple Values to Multiple Variables in Single Line
You can create multiple variables in a single line with their data type and value. To create multiple variables in a single, you have to add data type followed by the variable name and its value. The below example creates multiple variables in a single line of code.
1 |
int a = 6, b = 12, c = 13; |
To manipulate the values, you can use the below example to get the result of the addition of these variable values.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; namespace Demo { class Program { static void Main(string[] args) { const int a = 6, b = 12, c = 13; Console.WriteLine(a+b+c); } } } |
Output
The above result is the addition of multiple variables created in a single.
Reference