Advertisement

Responsive Advertisement

II. Variable

 II. Variable


A variable is a name given to a memory location and all the operations done on the variable effects that memory location. In C#, all the variables must be declared before they can be used. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. 

II.1. Variable Declaration

Used to declare a variable. Variables can be implicitly or explicitly typed.

Variables declared this way (without a static modifier) within classes are called instance variables. They belong to an instance of the class (i.e. an object).



II.2. Declaration Multiple Variables

This content is archived and is not being updated. For the latest documentation, see Microsoft Dynamics 365 product documentation. For the latest release plans, see Dynamics 365 and Microsoft Power Platform release plans.

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

 

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




II.3. Declaring Variable as needed for Loop

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

 

  • You can define a variable name with a combination of alphabets, numbers, and underscore.
  • A variable name must always start with either alphabet or underscore but not with numbers.
  • While defining the variable, no white space is allowed within the variable name.
  • Don't use any reserved keywords such as int, float, char, etc., for a 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.

The following are some valid ways to define the variable names in the c# programming language.




II.4. Multiple Equals Signs for Initialization

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

 

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

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

 

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





Post a Comment

0 Comments