C# Variables

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.

Syntax
data_type variable_name = value;

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

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

Output

9

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.

Output

12

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.

Output

9.8

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.

Output

a

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.

Output

This is a string.

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.

Output

true

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#.

Output

24

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.

To manipulate the values, you can use the below example to get the result of the addition of these variable values.

Output

31

The above result is the addition of multiple variables created in a single.

Reference

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.