Академический Документы
Профессиональный Документы
Культура Документы
Namespace:
Whenever we want to group similar classes into a single entity, we can define a
namespace.
Ex:
namespace Vehicle
{
public class Car
{
//members of Car class
}
public class Bus
{
//members of Bus class
}
public class Bike
{
//members of Bike class
}
}
using System;
using Vehicle; //note this
class Test
{
public static void Main()
{
Car c=new Car();
-------
-------
}
}
Resolving Namespace clashes:
There may be situation where more than one namespace contains the
class with same name. For example, we may have one more
namespace like –
namespace MyVehicle
{
public class Car
{
//members of Car class
}
--------
-------
}
using System;
using Vehicle;
using MyVehicle; class Test
{
public static void Main()
{
// Car c=new Car(); Error!!! name conflict
Declaring a method
Method Overloading:
If we need to do the same kind of the operation in different ways i.e. for different
inputs. In the example described below, we are doing the addition operation for
different inputs. It is hard to find many different meaningful names for single action.
Ex:
using System;
class GFG {
// Creating Object
GFG ob = new GFG();
Output:
sum of the two integer value : 3
sum of the three integer value : 6
• For example, if the left operand of the && operator evaluates to false, the
result of the entire expression must be false, regardless of the value of the
right operand.
• Similarly, if the value of the left operand of the || operator evaluates to
true, the result of the entire expression must be true, irrespective of the
value of the right operand.
• In these cases, the && and || operators bypass the evaluation of the right
operand.
.To String:
It converts an object to its string representation so that it is suitable for display.
using System;
Console.WriteLine(obj.ToString());
// System.Object
Precedence and Associativity:
C# if (if-then) Statement
C# if-then statement will execute a block of code if the given condition is true. The
syntax of if-then statement in C# is:
if (boolean-expression)
{
// statements executed if boolean-expression is true
If the Boolean-expression returns false, the statements inside the body of if will be
ignored.
EX:
using System;
namespace Conditional
{
class If Statement
{
public static void Main (string [] args)
{
int number = 2;
if (number < 5)
{
Console. WriteLine("{0} is less than 5",
number);
}
if (boolean-expression-1)
else if (boolean-expression-2)
else if (boolean-expression-3)
else
{
// statements executed if all above expressions are false
using System;
namespace Conditional
{
class IfElseIfStatement
{
public static void Main(string[] args)
{
int number = 12;
if (number < 5)
{
Console.WriteLine("{0} is less than 5",
number);
}
else if (number > 5)
{
Console.WriteLine("{0} is greater than 5",
number);
}
else
{
Console.WriteLine("{0} is equal to 5");
}
}
}
}
Switch Case in C#
A switch case is used test variable equality for a list of values, where each value is a
case. When the variable is equal to one of the cases, the statements following the case
are executed. A break statement ends the switch case. The optional default case is for
when the variable does not equal any of the cases.
Syntax
switch(expression) {
case valueOne:
//statements
break;
case valueTwo:
//statements
break;
default:
//optional
//statements
}
Example
switch(choice) {
case 'Y' :
Console.WriteLine("Yes");
break;
case 'M' :
Console.WriteLine("Maybe");
break;
case 'N' :
Console.WriteLine("No");
break;
default:
Console.WriteLine("Invalid response");
}
Loops in C#
Looping in programming language is a way to execute a statement or a set of
statements multiple number of times depending on the result of condition to be
evaluated to execute statements. The result condition should be true to execute
statements within loops.
1. while loop The test condition is given in beginning of loop and all statements
are executed till the given boolean condition satisfies, when the condition
becomes false, the control will be out from the while loop.
Syntax:
while (boolean condition)
{
loop statements...
}
EX:
using System;
class whileLoopDemo
int x = 1;
while (x <= 4)
Console.WriteLine("GeeksforGeeks");
// next iteration
x++;
Output:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
2. for loop
for loop has similar functionality as while loop but with different syntax. for
loops are preferred when the number of times loop statements are to be
executed is known beforehand.
Syntax:
class forLoopDemo
{
public static void Main ()
{
// for loop begins when x=1
// and runs till x <=4
for (int x = 1; x <= 4; x++)
Console.WriteLine("GeeksforGeeks");
}
}
Output:
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
3. do-while loop
do while loop is similar to while loop with only difference that it checks the condition
after executing the statements, i.e it will execute the loop body one time for sure
because it checks the condition after executing the statements.
Syntax :
do
{
statements..
}while (condition);
EX:
// C# program to illustrate do-while loop
using System;
class dowhileloopDemo
{
public static void Main()
{
int x = 21;
do
{
Console.WriteLine("GeeksforGeeks");
x++;
}
while (x < 20);
}
}
Output:
GeeksforGeeks
4. while loop
Syntax
The syntax of a while loop in C# is −
while(condition) {
statement(s);
}
using System;
namespace Loops {
class Program {
int a = 10;
a++; }
Console.ReadLine();
} } }
The break and continue statements:
The break statement is used to jump out of a switch statement also to jump out
of the body of an iteration statement.
Breaking out of a loop, the loop exits immediately and execution continues at
the first statement that follows the loop.
In contrast, the continue statement causes the program to perform the next
iteration of the loop immediately (after revaluating the Boolean expression).
EX:
C# Variables
A variable is a name given to a storage area that is used to store values of
various data types. Each variable in C# needs to have a specific type, which
determines the size and layout of the variable's memory.
Initializing Variables
Variables are initialized (assigned a value) with an equal sign followed by
a constant expression. The general form of initialization is −
<data_type> <variable_name> = value;
Syntax:
Different type of Exceptions:
EX:
using System;
namespace ErrorHandlingApplication {
class DivNumbers {
int result;
DivNumbers() {
result = 0;
try {
} catch (DivideByZeroException e) {
} finally {
d.division(25, 0);
Console.ReadKey();
}
PROGRAMS TO STUDY:
Factorial using Recursion:
Output
Value is : 362880
Largest of 3 numbers:
11. {
12. n3=n1+n2;
13. Console.Write(n3+" ");
14. n1=n2;
15. n2=n3;
16. }
17. }
18. }
Output:
O/P:
Operators in C#: