C# Variables with Examples

  Keywords : C# variables represent a storage location and store the particular type of values with examples, C# variable is used to hold the value of specified data type with examples

Here we will learn variables in c# with examples, c# variable declaration and initialization with examples and rules to declare a variables in c#, declare multiple and multiple variables in c# with examples.

C# Variables

In C#, Variables will represent a storage locations and each variable has a particular type that determines what type of values can be stored in the variable.

 

C# is a Strongly Typed programming language so before we perform any operation on variables, it’s mandatory to define a variable with required data type to indicate what type of data that variable can hold in our application.

Syntax of C# Variables Declaration

Following is the syntax of declaring and initializing a variables in c# programming language.

 

[Data Type] [Variable Name];

[Data Type] [Variable Name] = [Value];

[Access Specifier] [Data Type] [Variable Name] = [Value];

If you observe above syntax, we added a required data type before the variable name to tell the compiler about what type of data the variable can hold or which data type the variable belongs to.

 

[Data Type] - It’s a type of data the variable can hold such as integer, string, decimal, etc.

 

[Variable Name] - It’s a name of the variable to hold the values in our application.

 

[Value] - Assigning a required value to the variable.

 

[Access Specifier] - It is used to define an access permissions for the variable.

 

Now we will see how to define a variables in our c# applications with examples.

C# Variables Declaration Example

Following is the example of using the variables in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int number = 10;

            string name = "Suresh Dasari";

            double percentage = 10.23;

            char gender = 'M';

            bool isVerified = true;

            Console.WriteLine("Id: " + number);

            Console.WriteLine("Name: " + name);

            Console.WriteLine("Percentage: " + percentage);

            Console.WriteLine("Gender: " + gender);

            Console.WriteLine("Verified: " + isVerified);

            Console.ReadLine();

        }

    }

}

If you observe above c# variables example, we defined a multiple variables with different data types and assigned a values based on our requirements.

Output of C# Variables Declaration Example

When we execute above program by pressing Ctrl + F5 or click on Start option in menu bar we will get the result like as shown below.

 

C# Variables Declaration Example Result

 

If you observe above result, we are able to print the variables in our c# application based on our requirements.

Rules to Declare a C# Variables

Before we declare and define a variables in c# programming language, we need to follow particular rules.

 

  • We can define a variable name with the combination of alphabets, numbers and underscore.
  • A variable name must always starts with either alphabets or underscore but not with numbers.
  • While defining the variable, no white space is allowed within the variable name.
  • We should not use any reserve keywords such as int, float, char, etc. for variable name.
  • In c#, once the variable is declared with a particular data type, it cannot be re-declared with a new type and we shouldn’t assign a value that is not compatible with the declared type.

Following are the some of valid ways to define the variable names in c# programming language.

 

int abc;

float a2b;

char _abc;

Following are the some of Invalid ways of defining the variable names in c# programming language.

 

int a b c;

float 2abc;

char &abc;

double int;

C# Multiple and Multi Line Variables Declaration

In c#, we can declare and initialize a multiple variables of same data type in a single line by separating with a commas.

 

Following is the example of defining the multiple variables of same data type in a single line by separating with a commas in c# programming language.

 

int a, b, c;

float x, y, z = 10.5;

While declaring the multiple variables of same data type, we can arrange them in a multiple lines to make it more readable and the compiler will treat it as single statement until it encounters a semicolon (;).

 

Following is the simple of defining the multiple variables of same data type in multiple lines in c# programming language.

 

int a,

     b,

     c;

float x,y,

       z = 10.5;

C# Variables Assignment

In c#, once we declare and assign a value to the variable that can be assigned to another variable of same data type.

 

Following is the example of assigning a value of one variable to another variable of same type in c# programming language.

 

int a = 123;

int b = a;

string name = "suresh";

string firstname = name;

In c#, it’s manadatory to assign a value to the variable before we use it otherwise we will get a compile time errors.

 

In case, if we try to assign a value of string data type to integer data type or vice versa like as shown below, we will get an error like “cannot implicitly convert type int to string”.

 

int a = 123;

string name = a;

This is how we can use variables in c# programming language based on our requirements.