Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Basic Syntax in c# Language



A variable is a location in the memory that has a name and contains a value. Variable means the value can be changed. A variable name may contains ‘_’ (underscore) followed by a sequence of letters, digits(0-9), or underscores. The first character in a variable name cannot be a digit

//Variable Declaration
[data_type] [variable_name]=[value];

//Data types are two types.
Value types:- The values are directly containing the data.
Char, int and float are value types

Example:

int a = 10; // 'a' is a variable that contains the values 10.
char c=’S’; // 'c' is a character variable that contains the value 'S'.
float f=10.22; // 'f' is a float variable that contains the value 10.22

Reference types:- The reference types do not maintain the data but they contain reference to the variables.
String is a reference types

Example:

string siteName= "dotnetpgm.blogspot.com"; //string is a set of character.

Looping statements in c#

1. The for loop
2. The while loop
3. The do...while loop

The for loop structure is used to execute a block of statements for a specific number of times.

//for loop for 5 times iteration

int index = 0;
for ( int index = 0; index {
Response.WriteLine(index);
}

//for each is used for array of collection
string[] str = new string[]{ "dotnetblog", "dotnetpgm", "c#" }; //split the string by string by string

foreach(string c in str)
{ Response.WriteLine(c);
}

//while is used to check if the condition is true

int index = 0;
while ( index {
Response.WriteLine(index); ++index;
}

//do while is used to execute the code and then check the condition(only difference between with while)
int index = 0;
do
{
Response.WriteLine ( index );
index++;
}
while ( index
Using Conditional Constructs Conditional is used for decision making. The going path is right means we will go if wrong means we will stop there and analyse and then go in the right path. Like this

//the value is true
bool value=true;
if(value)
{
Response.WriteLine("Yes the value is True");
}
else
{
Response.WriteLine("No the value is False");
}

//switch is used for more conditions but one condition will execute
int index=3;
switch(index)
{
case 1:
Response.WriteLine("Index value is "+index);
break;
case 2:
Response.WriteLine("Index value is "+index);
break; case 3: Response.WriteLine("Index value is "+index);
break;
} //the break will quit if the condition execute.


This post first appeared on Dotnet, please read the originial post: here

Share the post

Basic Syntax in c# Language

×

Subscribe to Dotnet

Get updates delivered right to your inbox!

Thank you for your subscription

×