Sunday, November 15, 2020

Variables in C#

Variables are containers for storing data values.

In C#, there are different types of variables (defined with different keywords), for example:

int - stores integers (whole numbers), without decimals, such as 123 or -123

double - stores floating point numbers, with decimals, such as 19.99 or -19.99

char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

string - stores text, such as "Hello World". String values are surrounded by double quotes

bool - stores values with two states: true or false

Declaration of variable:

To declaring a variables, you must be specify the type of variable and assign it value.

Syntax:

type variable_Name = some value;

Where type is a C# type (such as int or string), and variable_Name is the name of the variable (such as x or name). The equal sign is used to assign values to the variable.

Example: - 1

Create the type string variable as name and assign the value john and print on console.

string name="john"

Console.WriteLine(name);

Example: - 2

Create the type int variable as myValue and assign the some numeric value.

int myValue=25;

Console.WriteLine(myValue);


Constants Variables in C#:

However, you can add the const keyword if you don't want others (or yourself) to overwrite existing values (this will declare the variable as "constant", which means unchangeable and read-only):

const int myValue=15;

Console.WriteLine(myValue);

myValue=25;// here give the Error

We can not assign new to constant variable.

The const keyword is useful when you want a variable to always store the same value, so that others (or yourself) won't mess up your code. An example that is often referred to as a constant, is PI (3.14159...).

Note: You cannot declare a constant variable without assigning the value. If you do, an error will occur: A const field requires a value to be provided.

C# Identifiers:

All C# variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and maintainable code:

Example:

int seconds=60;

//Good to understand

int s=60;

// OK, but not so easy to understand what m actually is


Note:
  1. Names can contain letters, digits and the underscore character (_)
  2. Names must begin with a letter
  3. Names should start with a lowercase letter and it cannot contain whitespace
  4. Names are case sensitive ("myVar" and "myvar" are different variables)
  5. Reserved words (like C# keywords, such as int or double) cannot be used as names

For more information about variables, Please visit Here

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...