Archive for December 2015
C#.NET Class
- Class is a blue print.
- It is a collection of methods and member functions.
- In Object oriented programming every line of code should be in class.
- It is a reference type.
- Mandatory to use the "new" keyword for creating object.
- Class can contain variable declarations as well as initialize them at the time of declaration only.
class classname
{
body
}
C#.NET Static Method
Static method is a method which can be called without any reference or object because memory for a static method is created in RAM when program is executed.
Static method is a method which is created using keyword "static".
Example
Addition of two numbers Program using static method and takes input arguments with return type
Program
Output
11
Static method is a method which is created using keyword "static".
Example
Addition of two numbers Program using static method and takes input arguments with return type
Program
using System;
namespace static_method
{
class Program
{
static void Main(string[] args)
{
long l = maths.add(4,
7);
Console.WriteLine(l);
Console.ReadKey();
}
class maths
{
public static long add(int i, int j)
{
return i + j;
}
}
}
}
Output
11
C#.NET Abstract method
Abstract method is a method which is created using "abstract"keyword.
It doesn't have any implementation or body.
It will have only signature.
Semicolon is required at the end of signature.
It is implemented in the class which inherits it.
Syntax for creating abstract method
abstract keyword access specifier return_type method_name(input parameters)
Example
abstract public long add(int a,int b);
It doesn't have any implementation or body.
It will have only signature.
Semicolon is required at the end of signature.
It is implemented in the class which inherits it.
Syntax for creating abstract method
abstract keyword access specifier return_type method_name(input parameters)
Example
abstract public long add(int a,int b);
C#.NET Method with arguments and return type
It is one of the method that contains both arguments and return type.
Example
public long add(int a,int b)
{
return a+b;
}
Example
public long add(int a,int b)
{
return a+b;
}
C#.NET Method with Return Type
It is one of the method that doesn't have any arguments/parameters. It contains only return type.
Example
public int add()
{
Console.WriteLine("addition");
return 1;
}
A method can return value to anything which is capable of holding that value.
Example
int i=add();
Console.WriteLine(i);//1
Explanation
In the above example the add method can return value'1' to i and it will be printed as output.
Example
public int add()
{
Console.WriteLine("addition");
return 1;
}
A method can return value to anything which is capable of holding that value.
Example
int i=add();
Console.WriteLine(i);//1
Explanation
In the above example the add method can return value'1' to i and it will be printed as output.
C#.NET Method with arguments
It is one type of method and consists of arguments/parameters.
Example
public long add(int a,int b)
{
Console.WriteLine(a+b);
}
Explanation
In the above example the add() can take two values of int type as inputs and doesn't return any type because long data type is a return type but in body section there is no return type.
Example
public long add(int a,int b)
{
Console.WriteLine(a+b);
}
Explanation
In the above example the add() can take two values of int type as inputs and doesn't return any type because long data type is a return type but in body section there is no return type.
C#.NET Simple Method
Simple method is a method that doesn't have any arguments and return type.
Syntax
Access specifier return type methodname( )
Example
public void add()
{
Console.WriteLine("addition");
}
Syntax
Access specifier return type methodname( )
Example
public void add()
{
Console.WriteLine("addition");
}
C#.NET Methods
- Method is a piece of code which is used to perform a particular task.
- A Method can be either no value returning or value returning.
- If it was a no value returning we will use void as return type for that method.
- If it was a value returning we use a specific type i.e., what type of value that a method returns.
Access specifier return type methodname( )
{
}
Access Specifier : It is used to define the accessibility level of a method.
Example for access specifiers are public, private, protected etc.
ReturnType: What type of value return by a method is given as return type.
Method name: The name of a method to call it.
Example
public void add()
{
//Code Logic
}
There are different types of methods
- C#.NET Simple Method
- C#.NET Method with arguments
- C#.NET Method with return type
- C#.NET Method with arguments and return type
- C#.NET Abstract Method
- C#.NET Static Method
C#.NET OOPS
OOPS stands for Object Oriented Programming Structure.
It was a new approach in programming introduced in 1970's when a suffered problems with procedural programming languages like lack of security and reusability.
Object Oriented approach provides us security and reusability of the code.
To call a language as Object Oriented it needs to satisfy a set of rules prescribed by an approach. Those are:
It was a new approach in programming introduced in 1970's when a suffered problems with procedural programming languages like lack of security and reusability.
Object Oriented approach provides us security and reusability of the code.
To call a language as Object Oriented it needs to satisfy a set of rules prescribed by an approach. Those are:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
C#.NET Jump Statements
C# provides a jump statements that allow you to jump immediately to another line in a program.
They are:
1. goto
2. break
3. continue
It is used to exit from loops like for, for each, while and do while which will switch the control to the statement immediately after the end of the loop.
They are:
1. goto
2. break
3. continue
goto
It allows you to jump directly to another specified line in the program indicated by a label which is an identifier followed by a semi colon;break
It is used to exit from a case in switch statement.It is used to exit from loops like for, for each, while and do while which will switch the control to the statement immediately after the end of the loop.
continue
This can be used only in loops which will jump the control to the iteration part without executing the statements present after it.C#.NET Do While
Do while loop executes a set of statements inside it atleast once and repeated till the condition is met.
Syntax
Do
{
<statements>
} while(condition);
C#.NET DoWhile Example program
Program
Output
0
1
2
3
4
5
6
7
8
9
Syntax
Do
{
<statements>
} while(condition);
C#.NET DoWhile Example program
Program
using System;
namespace DoWhileLoop
{
class Program
{
static void Main(string[] args)
{
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 10);
Console.ReadKey();
}
}
}
Output
0
1
2
3
4
5
6
7
8
9
C#.NET While loop
While loop is used to execute a set of statements till the condition is met. It doesn’t have initialization and increment or decrement operators as part of its syntax.
Syntax
While(condition)
{
<Statements>
}
C#.NET While loop program
Program
Output
0
1
2
3
4
5
6
7
8
9
Examples
1
Amstrong number because 1^3 = 1
121
Not amstrong number because 1^3+2^3+1^3 = 1+8+1= 10.
10 not equal to 121
153
Amstrong number because 1^3+5^3+3^3 = 1+ 125 + 27 =153
153 equal to 153.
C#.NET program to say Armstrong or not
using System;
Explanation: In this program the number entered is stored in a variable num.
The value inside variable num is copied into variable num1 to match the result with this number at the last.
While loop is used to retreive the last number of the given number and to find the cube of that number which are added and stored in variable sum.
After coming out of while loop the value in sum and num1 is matched to know wheather the given number is amstrong or not.
If they are matched the value given is amstrong otherwise not.
Syntax
While(condition)
{
<Statements>
}
C#.NET While loop program
Program
using System;
namespace ConsoleTasks
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i < 10)
{
Console.WriteLine(i);
i++;
}
Console.ReadKey();
}
}
}
Output
0
1
2
3
4
5
6
7
8
9
Armstrong Number program
If the sum of the cubes of digits in a number is equal to that number then it is known as Armstrong number.Examples
1
Amstrong number because 1^3 = 1
121
Not amstrong number because 1^3+2^3+1^3 = 1+8+1= 10.
10 not equal to 121
153
Amstrong number because 1^3+5^3+3^3 = 1+ 125 + 27 =153
153 equal to 153.
C#.NET program to say Armstrong or not
using System;
namespace Armstrong
{
class Program
{
static void Main(string[] args)
{
int num, num1, temp, q, sum = 0;
num
= Convert.ToInt32(Console.ReadLine());
num1
= num;
while (num > 0)
{
q = num / 10;
temp = num % 10;
sum = sum + (temp * temp * temp);
num = q;
}
if (sum == num1)
{
Console.WriteLine("{0} is Armstrong ");//153
}
else
{
Console.WriteLine("{0} is not Armstrong");//121
}
Console.ReadLine();
}
}
}
Explanation: In this program the number entered is stored in a variable num.
The value inside variable num is copied into variable num1 to match the result with this number at the last.
While loop is used to retreive the last number of the given number and to find the cube of that number which are added and stored in variable sum.
After coming out of while loop the value in sum and num1 is matched to know wheather the given number is amstrong or not.
If they are matched the value given is amstrong otherwise not.
C#.NET foreach loop
foreach loop is a special loop used for collections and arrays. It is repeated based on the number of elements in a collection. It automatically takes one by one element from a collection and it is repeated till the last element is printed.
Syntax for C# foreach loop
foreach(type variable in collect:ion/array)
{
statements
}
Example program using C# foreach loop
Program
Output
7
8
9
C#.NET program to print how many times a character is present in a string
Output
3
Explanation
above program contains a string variable s which contain "arshadmohammod"
In the above string a is repeated 3 times arshadmohammod
That string is divided into an array of characters using method ToCharArray( ) . -
char[ ] c = s.ToCharArray( );
count is a variable of int type intialized with zero used to count no of a's repeated in the string.
using for each loop all elements in char array are stored into ch one by one and repeatedly checked using if condition.
if (ch == 'a')
{
count++;
}
if element matched with a then count is incremented by one.
after the foreach loop the number in count variable is printed as output.
Syntax for C# foreach loop
foreach(type variable in collect:ion/array)
{
statements
}
Example program using C# foreach loop
Program
using System;
namespace loops
{
class Program
{
static void Main(string[] args)
{
int[] i = new int[3] { 7, 8, 9 };
foreach (int a in i)
{
Console.WriteLine(a);
}
Console.ReadKey();
}
}
}
7
8
9
C#.NET program to print how many times a character is present in a string
using System;
namespace Chars
{
class Program
{
static void Main(string[] args)
{
string s = "arshadmohammod";
char[] c = s.ToCharArray();
int count = 0;
foreach (char ch in c)
{
if (ch == 'a')
{
count++;
}
}
Console.WriteLine("no of times a repeated is
:" + count);
Console.ReadKey();
}
}
}
3
Explanation
above program contains a string variable s which contain "arshadmohammod"
In the above string a is repeated 3 times arshadmohammod
That string is divided into an array of characters using method ToCharArray( ) . -
char[ ] c = s.ToCharArray( );
count is a variable of int type intialized with zero used to count no of a's repeated in the string.
using for each loop all elements in char array are stored into ch one by one and repeatedly checked using if condition.
if (ch == 'a')
{
count++;
}
if element matched with a then count is incremented by one.
after the foreach loop the number in count variable is printed as output.
C#.NET for loop
C#.NET for loop is used to execute a code repeatedly until the condition is true. For loops are appropriate when you know exactly how many times you want to perform the statements within the loop. Normally, for loop statements execute from the opening curly brace to the closing curly brace without interruption. Every for loop can contain three sections commonly.
Those sections are
Initialization: Which sets the starting point of the loop.
Condition: Once the initializer list has been evaluated, the for loop gives control to its second section, ie.,the boolean expression. The code is executed until the condition satisfied ie.,until it returns true value.
Iteration(Increment/Decrement): Once Condition has been evaluated, the for loop gives to its control to third section ie.,Iteration, Which takes to the next side either positive or negative direction. Sometimes it will be incremented, sometimes decremented depending on the situation.
Syntax for C#.NET for loop
for(initialize; condition; inc/dec)
{
statements
}
Example program to print even numbers between 10 and 45 using for loop and if condition
Program
Output
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
Example program using C#.NET for loop
Program
Output
*
**
***
****
C#.NET program to print stars in triangle format
Output required: Print a message to ask user to enter the number of rows based on which stars are printed.
In the first row there should be one star, In the second row 2 stars, In the third row 3 stars,... as shown in the below image.
Program
Output
Enter no. of rows
4
*
**
***
****
Those sections are
Initialization: Which sets the starting point of the loop.
Condition: Once the initializer list has been evaluated, the for loop gives control to its second section, ie.,the boolean expression. The code is executed until the condition satisfied ie.,until it returns true value.
Iteration(Increment/Decrement): Once Condition has been evaluated, the for loop gives to its control to third section ie.,Iteration, Which takes to the next side either positive or negative direction. Sometimes it will be incremented, sometimes decremented depending on the situation.
Syntax for C#.NET for loop
for(initialize; condition; inc/dec)
{
statements
}
Example program to print even numbers between 10 and 45 using for loop and if condition
Program
using System;
namespace For
{
class Program
{
static void Main(string[] args)
{
for (int i = 10; i <
45; i++)
{
if (i % 2 == 0)
{ Console.WriteLine(i); }
}
Console.ReadKey();
}
}
}
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
Example program using C#.NET for loop
Program
using System;
namespace loops
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 4;
i++)
{
for (int j = 0; j < i
+ 1; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Output
*
**
***
****
C#.NET program to print stars in triangle format
Output required: Print a message to ask user to enter the number of rows based on which stars are printed.
In the first row there should be one star, In the second row 2 stars, In the third row 3 stars,... as shown in the below image.
Program
using System;
namespace PrintingStars
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter no. of rows");
//taking no. of rows from user
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <=
n; i++)
{
for (int j = 0; j < i;
j++)
{ Console.Write("*"); }
//for moving to second row
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Enter no. of rows
4
*
**
***
****
C#.NET Conditional Looping
C#.NET provides four different loops that allows you to execute a block of code repeatedly until a certain condition is met.
Those are as follows
1) for loop
2) while loop
3) dowhile loop
4) foreach loop
Those are as follows
1) for loop
2) while loop
3) dowhile loop
4) foreach loop
C#.NET Switch
The switch statement which allows you to compare an expression with a number of different values.
Syntax of C#.NET Switch statement
switch(variable)
{
case value1:
statements;
break;
case value2:
statements;
break;
-------------------
-------------------
-------------------
default:
statements;
break;
Example program using C#.NET switch statement
Program
Syntax of C#.NET Switch statement
switch(variable)
{
case value1:
statements;
break;
case value2:
statements;
break;
-------------------
-------------------
-------------------
default:
statements;
break;
Example program using C#.NET switch statement
Program
using System;
namespace Switch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter number");
int no = Convert.ToInt32(Console.ReadLine());
switch (no)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
default:
Console.WriteLine("Invalid");
break;
}
Console.ReadKey();
}
}
}
Output
Enter number
1
One
Output
Enter number
2
Two
Output
Enter number
12
Invalid
Explanation
In the above example we can give input is 1 it prints output as One, input is 2 print output as Two, if we can give input is any number except 1 and 2 the output prints Invalid because if we can give input is 1 compiler executes case1 and we give i/p is 2 executes case2 then print output as One and Two. We give any other number except 1 and 2 default case will execute and prints Invalid.
Example program for C#.NET switch statement
Program
using System;
namespace Switch2
{
class Program
{
static void Main(string[] args)
{
int i = 4;
switch (i)
{
case 1:
case 2:
case 3:
Console.WriteLine("One");
break;
case 4:
case 5:
Console.WriteLine("Two");
goto case 1;
default:
Console.WriteLine("Invalid");
break;
}
Console.ReadKey();
}
}
}
Output
Two
One
Explanation
In the above example we can assign i value as 4. So directly case 4 is executed and print output is Two, next no break statement is available. instead of break we write goto statement as goto case1,then after the execution go and starts from case1 then the output one will be printed. Finally the output will be Two and One printed line by line.