## C# Tutorial

In this lesson, we will learn how to work with variables and constants in C#. Let us first begin with variables:Variables in C#Variables are the names used for the storage areas that are manipulated by the programs to obtain various results. There are different types of variables such as integral, floating-point, boolean, character etc. The variable type determines the size and layout of the variable memory.Variable Definition and InitializationA variable can be defined using the following syntax:DataType VariableList ;Here, “DataType” can be any valid C# data type such as int, float, double, char, bool, etc.         “VariableList” may contain one or more variable names separated by commas.Variables can be initialized using the assignment operator with the variable name to its left and the value assigned to its right. This is shown as follows:VariableName = Value;Here, “VariableName” is the name of the variable.         “Value” is the value assigned to the variable.Variable initialization can also be done at the time of variable declaration. The syntax for that is as follows:DataType VariableName = Value;Here, “DataType” can be any valid C# data type such as int, float, double, char, bool etc.         “VariableName” is the name of the variable.         “Value” is the value assigned to the variable.A C# program that demonstrates variable definition and initialization is as follows:Source Code: Program to work with variables in C#using System; namespace Demo {   public class Example {      public static void Main(string[] args) {      int a =10;      int b;      b=15;      Console.WriteLine("Value of a is {0}",a);      Console.WriteLine("Value of b is {0}",b);      }   } }The output of the above program is as follows:Value of a is 10 Value of b is 15Variable Naming ConventionsThe variable naming conventions in C# are as follows:A variable should only start with an alphabet or underscore. It should not start with a digit.A variable may contain alphabets, digits as well as underscore.There can be no whitespace in a variable name.A variable name cannot contain any keywords such as int, const, switch etc.A list of valid and invalid variable names is as follows:int var;                             // Valid int _var;                           // Valid int var1;                           // Valid int name_var_1;              // Valid int 10;                              // Invalid int 10val;                         // Invalid int name var 1;                // Invalid int switch;                        // InvalidReading input from ConsoleThe function ReadLine() in the Console class in the System namespace is used to accept input from the user and store it into a variable. This can be done by writing Console.ReadLine() to accept the user input. However, this accepts the input in string format and so Convert.ToInt32() is used in the case of int data types to convert the input to int.A program to demonstrate this is as follows:Source Code: Program to read input from Console in C#using System; namespace Demo { public class Example {  public static void Main(string[] args) {   int val;   val = Convert.ToInt32(Console.ReadLine());   Console.WriteLine("Value of val is {0}", val);  } } }The output of the above program for input value 5 is:Value of val is 5Constants in C#Constants in a program are fixed values that cannot be altered once they are given. They can be an integer constant, floating constant, character constant or string literal.Constants can be defined using the const keyword.The syntax is given as follows:const DataType ConstantName = value; Here, “const” is the keyword          “DataType” can be any valid C# data type such as int, float, double, char, bool etc.          “ConstantName” is the name given to the constant.          “value” is the value assigned to the constantA program that demonstrates the constant data type is as follows:Source Code: Program to implement constant data types in C#using System;   namespace Constants {    public class Example {       public static void Main(string[] args) {             const double pi = 3.14159;   // constant declaration           double r;                  r = Convert.ToDouble(Console.ReadLine());                       double area = pi * r * r;          double circumference = 2 * pi * r;             Console.WriteLine("Area of circle is: {0}",area);          Console.WriteLine("Circumference of circle is: {0}",circumference);                }    } }The output of the above program is as follows:Area of circle is: 78.53975 Circumference of circle is: 31.4159

# Variables and Constants in C#

In this lesson, we will learn how to work with variables and constants in C#. Let us first begin with variables:

## Variables in C#

Variables are the names used for the storage areas that are manipulated by the programs to obtain various results. There are different types of variables such as integral, floating-point, boolean, character etc. The variable type determines the size and layout of the variable memory.

## Variable Definition and Initialization

A variable can be defined using the following syntax:

DataType VariableList ;

Here, “DataType” can be any valid C# data type such as int, float, double, char, bool, etc.
“VariableList” may contain one or more variable names separated by commas.

Variables can be initialized using the assignment operator with the variable name to its left and the value assigned to its right. This is shown as follows:

VariableName = Value;

Here, “VariableName” is the name of the variable.

“Value” is the value assigned to the variable.

Variable initialization can also be done at the time of variable declaration. The syntax for that is as follows:

DataType VariableName = Value;

Here, “DataType” can be any valid C# data type such as int, float, double, char, bool etc.
“VariableName” is the name of the variable.

“Value” is the value assigned to the variable.

A C# program that demonstrates variable definition and initialization is as follows:

Source Code: Program to work with variables in C#

using System;
namespace Demo {
public class Example {
public static void Main(string[] args) {
int a =10;
int b;
b=15;
Console.WriteLine("Value of a is {0}",a);
Console.WriteLine("Value of b is {0}",b);
}
}
}

The output of the above program is as follows:

Value of a is 10
Value of b is 15

### Variable Naming Conventions

The variable naming conventions in C# are as follows:

2. A variable may contain alphabets, digits as well as underscore.
3. There can be no whitespace in a variable name.
4. A variable name cannot contain any keywords such as int, const, switch etc.

A list of valid and invalid variable names is as follows:

int var;                             // Valid
int _var;                           // Valid
int var1;                           // Valid
int name_var_1;              // Valid
int 10;                              // Invalid
int 10val;                         // Invalid
int name var 1;                // Invalid
int switch;                        // Invalid

The function ReadLine() in the Console class in the System namespace is used to accept input from the user and store it into a variable. This can be done by writing Console.ReadLine() to accept the user input. However, this accepts the input in string format and so Convert.ToInt32() is used in the case of int data types to convert the input to int.

A program to demonstrate this is as follows:

Source Code: Program to read input from Console in C#

using System;
namespace Demo {
public class Example {
public static void Main(string[] args) {
int val;
Console.WriteLine("Value of val is {0}", val);
}
}
}

The output of the above program for input value 5 is:

Value of val is 5

## Constants in C#

Constants in a program are fixed values that cannot be altered once they are given. They can be an integer constant, floating constant, character constant or string literal.

Constants can be defined using the const keyword.

The syntax is given as follows:

const DataType ConstantName = value;

Here, “const” is the keyword
“DataType” can be any valid C# data type such as int, float, double, char, bool etc.
“ConstantName” is the name given to the constant.

“value” is the value assigned to the constant

A program that demonstrates the constant data type is as follows:

Source Code: Program to implement constant data types in C#

using System;

namespace Constants {
public class Example {
public static void Main(string[] args) {

const double pi = 3.14159;   // constant declaration
double r;

double area = pi * r * r;
double circumference = 2 * pi * r;

Console.WriteLine("Area of circle is: {0}",area);
Console.WriteLine("Circumference of circle is: {0}",circumference);

}
}
}

The output of the above program is as follows:

Area of circle is: 78.53975
Circumference of circle is: 31.4159

## Swift Tutorial

Introduction to Swift Tutorial
13542
Swift Tutorial

Introduction to Swift Tutorial

R Programming

Python Tutorial
5394
Python Tutorial

Python Tutorial