Wednesday, October 14, 2020

C# Variables and Their Data Types

Introduction

Variables are temporary data storage memory locations which store program data.

The variable has a name and type. C# is a strongly typed language that means we must declare variable type at the time of declaration.

Rules For Declaring Variable Name

Variable Name must start with an English alphabet letter, _ or @ symbol. The first letter can be either lower case or upper case.

Example of Valid Variable Name declarations: 

fullName;

FullName;

@FullName;

_EmpSalary;

In the middle or at the end of Variable names we can also use English letters that can be either lower or upper case, numbers and underscore ( _ ).

Variable Names are case sensitive, that means someVar and somevar considered as two different variables.

No white space is allowed between variable name, means that full Name is an invalid variable name but we can use underscore ( _ ) character to join words example full_Name is a valid variable name. We can also use camel case to join two words where the first character of second word must be capital for example fullName.

We can use numbers in variable Name but not at the beginning, but we can use numbers in the middle or in the last character example 1stnumber is invalid but number1 is a valid variable name.

We can not use C# keywords as a variable name. It is recommended to choose a variable name in a way that it describes the value that they stored, for example,  courseName is a good choice for storing course name.

Example of Variable Declaration:

syntax of variable declaration

<data_type>  <variable_name>

Where data type must me valid C# data type (we will discuss soon) and variable name must be a valid identifier as we already discussed above.

string courseName;

where string is a data type and courseName is the name of our variable.

where semicolon ; indicates the end of statement in C#.

How we assign value to our  variable.

variable_name = value_to_assign

we always put our variable name on the left side and value to assign the variable on the right side and use operator is also called the assignment operator between them.

courseName = "C# Programming";     <------ The assigned value is in double quotes

The Point to be noted here that we put the assignment value, in the inverted ( " " ) commas, because our variable type is a string and string is nothing but stores the array of characters.

When we execute the following C# code it will print the value of our variable.

Console.WriteLine(courseName);

OUTPUT : C# Programming


Alternative way to declare a variable

string courseName = "C# Programming";

In the above statement, we assigned the value to our variable at the time of declaration.

How To store a single character

For storing a single character we used char data type in C#

char yourGrade = 'A';     <--------- The assigned value is in single quotes

Where char is a data type and yourGrade is a variable name.

We always assign a value to char data type within a single quote as opposite to a  string which requires double quote.

When we execute the following code.

Console.WriteLine("Your Grade is: " + yourGrade);

Then the output will be:

Your Grade is: A 


How to store numbers in C#

For storing whole numbers in C# we used Integer data type.

Example of Integer variable declaration

int number1;

int number2;

Where int is Integer data type and number1 and number2 are our variable's name.

How To Add Two numbers in C#

int sum;    <-------- here we declared a third variable which stores the sum of our two variables.

number1 = 20; <---------- here we assign the value to variable number1

number2 = 4; <--------- here we assign the value to variable number2

The most important point to be noted here that when we assigned the values to our Integer variables we were not using (" "), because it is only used when data type is string.

sum = number1 + number2;

When we run the following command 

Console.WriteLine("Sum of Two numbers is : " + sum);

The Output of the above code will be Sum of Two numbers is :  24

Where "Sum of Two numbers is :" is a string value which we are providing to our Console.WriteLine( ) and we used the + sign and then our variable name sum. Here our variable value is passed as a string to Console.WriteLine( ) and we used + sign because we are concatenating two strings.

We can also use long data type to store whole numbers, but only when need to store very large values.

How to work with Real Numbers in C#

To store real numbers we mostly use three data types

float height;

double pi;

decimal weight;

where floatdouble and decimal are data types and height, pi and weight are variables name.

The actual difference between these three data types is that how big decimal value they can store.

Here we assigned values to our variables storing real numbers

height = 4.9;

pi = 3.14159;

How To Work With Logical Variables in C#:

For logical values we use bool data type in C#. The yes value represents true and no value represents as false in bool data type.

Example of bool 

bool IamADoctor = false;

bool IamAProgrammer = true;

Where bool is a logical data type

When we execute the following code in C# 

Console.WriteLine(“Is I am a Doctor :  + IamADoctor);

Console.WriteLine(“Is I am a Programmer :  + IamAProgrammer);

It will produce the following output.

OutPut 

Is I am a Doctor : false

Is I am a Programmer : true


Declaring Multiple Variables

We can also declare multiple variables in a single line.

For Example:

int a,b,c,d;

Where int is our data type and a,b,c,d are our variable names each separating by comma (,).

Below is a C# Data Type Table given so you have a better idea.


These are some basic Data Types which we had discussed here, there are some other Data Types too. Which we will discuss in our upcoming articles as we need them. 

Here is a YouTube video Link of my C# Variables And Their Data Types tutorial (Urdu/Hindi language)


Written By

Syed Sohail Ahmed Quadri

YouTube Channel

Right Way Learning

Introduction To Full Stack Dot Net Development

  WHAT IS MEAN BY FULL STACK DEVELOPER A software engineer who works both on the Front End like (Angular, HTML, JavaScript, CSS, Bootstrap,...