Home » Blog » C# – Variables – Part 3

C# – Variables – Part 3

variables

In this article, we will be learning about C# variables.

Let’s get started.

Variables

As the name suggests, variables are used to store values. 

Let’s see it in action.

//C# Variables
int number1 = 100;

Here number1 is a variable name which is of integer type (denoted by int), the value which it stores is 100.

int is a data type, in C# every variable has to be of some type.

We will be deep diving into data types later.

Let’s see more examples.

//C# Variables
int number1 = 100;

float number2 = 300.56F;

decimal number3 = 300.05M;

double number4 = 1000.567;

string name = "John Cena";

string city = "Chicago";

bool isLearningFun = true;

char firstCharacter = 'A';

With the above examples,we see that the way to create a variable is:

type variableName = value;

The variable names should be meaningful and follow camel casing, i.e. even when variables are named as a,b,c,x,y,z they would work, but the code will become verbose.

Let’s see a few examples.

//Meaningful variable names

bool isAPermanentEmployee = true;

bool xyz = true;

The variable name isAPermanentEmployee makes more sense, xyz would also work, but it doesn’t provide any information as to what the boolean is all about.

int price = 200;
int quantity = 10;
int totalPrice = price* quantity;

Console.WriteLine("The total price of goods is: {0}", totalPrice);

In the above example, price, quantity, totalPrice are the apt variable names, that make the whole code snippet meaningful and readable.

Code should be beautiful to read, and meaningful variable names play a vital part in the readability.

Tags:

Leave a Reply

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