C# VARIABLES

C# VARIABLES


Variables is like a container in any programming language to storing the value .Value will be of any type.

In C# there are different types of of variables define with different keywords.They are as follows.

Int  - Int store integer values (whole numbers ) without decimal such 125 or -125.

Float - Float store decimal values such as 12.5 or-12.5.

Char - Char store single characters such as 'A 'or 'B ' with single quotes.

String - String store text such as "Hello World". String value surrounded by double quotes.

Bool - stores values with two states: true or false.

 

CREATING VARIABLE

To create a variable we must have to specify type and asssign value to it. Here is example for understand this.


Here value assign at the time of declaring.We can assign value later also.

String name;
name ="Ali"
console.writeline(name)

If we assign value at the time of declaring and after we assign another value to it it will overwrite the previous value.

Int Number=20;
Number=25;
Console.writeline(Number)

This example overwrite value from previous value with 25.




CONSTANTS

If you don't want others (or yourself) to overwrite existing values, you can add the const keyword in front of the variable type.This will be constant value(fixed value).

Const Int Number=10;
Number=20;

This change will throw error cause value is assign with const keyword.Also we cannot declare Const variable without assign value to it.



DISPLAY VARIABLES

The writeline method is often used to display variable vaues to the console window that we have seen in above examples.

For the combination of text and variable use + operator .

+ operator is also used to to add a variable to another variable.

string firstName = "Yusuf";

         string lastName = "Khan";

         string fullName = firstName + lastName;

         Console.WriteLine(fullName);

For numeric values + operator is works like mathematical operator.


MULTIPLE VARIABLES

To declare more than one variable of same type in one line use comma to separate them.

We can also assign same value to multiple variables in one line.

 int x, y, z;

          x = y = z = 30;

          Console.WriteLine(x + y + z);

 


IDENTIFIERS

All C# variables must be identified with unique names and these unique names are called Identifiers.

Identifiers are short name also like (a or b) as well as long name also like(MyName or My Number).

It is a good practice to use appropriate identifier as per our code for concise and understable reading.

The general rules for naming variables are:

Names can contain letters, digits and the underscore character (_).

Names must begin with a letter or underscore.

Names should start with a lowercase letter, and cannot contain whitespace.

Names are case-sensitive ("myAge" and "myage" are different variables).

Reserved words (like C# keywords, such as int or double) cannot be used as names.

In Summary we saw Definition of Variables,how to create them,Display Variables,Multiple Variables,Constants and at last Identifiers.

Variables are fundamental in any programming language for run our code perfectly, Understand our code properly and for better maintanability.

Previous Next

Start Your Data Journey Today With MSAInfotech

Take the first step towards data-led growth by partnering with MSA Infotech. Whether you seek tailored solutions or expert consultation, we are here to help you harness the power of data for your business. Contact us today and let’s embark on this transformative data adventure together. Get a free consultation today!

We utilize data to transform ourselves, our clients, and the world.

Partnership with leading data platforms and certified talents

FAQ Robot

How Can We Help?

Captcha

MSA Infotech