Chapter 2 .Net
Chapter 2 .Net
Topics:
1. Data Types, Type Casting, Boxing and Unboxing
2. Operators
3. Conditional Structure, Looping Structure
4. Arrays, Params Parameter
5. Pass arguments by value, ref and out
6. Method overloading
7. Access Modifiers
5 0 through +/-
79,228,162,514,264,337,593,543,950,335
Decima 16 bytes
l (+/-7.9...E+28) with no decimal point; 0 through
+/- 7.9228162514264337593543950335 with 28
places to
6 -1.79769313486231570E+308 through -
8 bytes on
64-bit
platform
13 Depends on
implementi
String ng platform 0 to approximately 2 billion Unicode characters
⮚ When the value of operand with higher type is assigned to the operand with
lower type then there may be loss of accuracy and this conversion is called
narrowing conversions. When option strict is on, narrowing conversion can’t be
done.
⮚ Narrowing conversion can be done only when option strict is off. to convert its
argument to the expression is used to such as [Link],
[Link], [Link], Convert.ToInt32 (int) and
Convert.ToInt64 (long)
2. Operators
⮚ It is a sign which commands the compiler to perform mathematical or logical
manipulations.
If ! (11 =1 2) {
Example:
}
4 || one of the sides is true, it returns true. if the
condition is false then it returns false.
If (condition)
{
//statements
}
Example 1:
Using System;
class ifstatement
{
public static void Main()
{
int a, b;
[Link]("Enter the value of a:” ) ;
a = [Link]([Link]());
[Link]("Enter the value of b: ");
b = [Link]([Link]());
If (a>b)
{
[Link](“a is greater
than b”);
}
[Link]();
}
}
3.1.2 If/Then/Else Selection Structure
⮚ The If/ Then statement shows that the code of the if block executed whether the
condition is true otherwise the else block of code is executed. the conditional
operators are used with the particular condition.
Syntax:
If (condition)
{
\\Statements
}
Else
{
\\Statements
}
Example 1:
Using System;
class ifstatement
{
public static void Main()
{
int a, b;
[Link]("Enter the value of a:” ) ;
a = [Link]([Link]());
[Link]("Enter the value of b: ");
b = [Link]([Link]());
If (a>b)
{
[Link](“a is greater than b”);
}
Else
{
[Link](“b is greater than a”);
}
[Link]();
}
}
⮚ Nested If..Then..Else statement is used to test more than one condition. it is inside
one another in the if then else statement.
Syntax:
If (Condition)
{
If (Condition)
{
[Statements]
}
Else
{
[Statements]
}
Else
{
[Statements]
}
Example 1. :
Using System;
class ifstatement
{
public static void Main()
{
int a, b,c;
[Link]("Enter the value of a:” ) ;
a = [Link]([Link]());
[Link]("Enter the value of b: ");
b = [Link]([Link]());
[Link]("Enter the value of c: ");
c = [Link]([Link]());
If (a>b)
{
If (a>c)
{
[Link](“a is greater”);
Else
{
[Link](“c is greater”);
}
}
Else
{
If (b>c)
{
[Link](“b is greater”);
}
Else
{
[Link](“c is greater”);
}
}
}
3.1.4 Else- If Ladder:
⮚ The condition is used with the else if ladder and test multiple conditions. if the
condition is true then if part is executed and false then elseif condition tested in
order and continue to check conditions. the statement is ending with the End If
keyword.
Syntax:
If (condition1)
{
‘Statements1
}
ElseIf (condition2)
{
‘Statement2
}
ElseIf (condition3)
{
‘Statement3
}
…
…
ElseIf (Condition N)
{
‘Statement N
}
Else
{
‘Statement Else
}
Example:
Using System;
class ifstatement
{
public static void Main()
{
int a, b,c;
[Link]("Enter the value of a:” ) ;
a = [Link]([Link]());
[Link]("Enter the value of b: ");
b = [Link]([Link]());
[Link]("Enter the value of c: ");
c = [Link]([Link]());
if(a>b)
{
[Link]("a is greater");
}
else If(a< b)
{
[Link]("b is greater");
}
else
{
[Link]("both are Equals");
}
[Link]();
}
Example
Class switch
{
public static void Main()
{
int a, b, choice;
Switch (choice)
{
Case 1 :
[Link]( "addition is" & a + b);
break;
Case 2 :
[Link]( "subtraction is" & a - b);
break;
Case 3 :
[Link]( "multiplication is" & a * b);
break;
Case 4 :
[Link]( "division is" & a / b);
break;
}
[Link]();
}
Do
{
//statement
}
While(Condition)
Example:
Class forexp
{
public static void Main()
{
int i=0;
While (i < 100)
{
[Link](i);
i += 10
}
}
}
Output:
0
10
20
30
40
50
60
70
80
90
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
[Link](i);
}
}
}
}
Output:
4 Arrays:
⮚ An array is the fixed -size sequential collection of elements which stores the
same type of array . An array is used to store a collection of data. Arrays start
from zero. So the first number of array is array name(0)
⮚ To declare an array datatype[] arrayName;
Example:
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
/* an array with 5 rows and 2 columns*/
int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
int i, j;
Output:
a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[4,1]: 8
Example:
using System;
class MyArray {
static void Main(string[] args) {
}
}
Output:
1
34
567
56
9
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
int x = 10;
[Link]("Variable Value Before Calling the Method: {0}", x);
Multiplication(x);
[Link]("Variable Value After Calling the Method: {0}", x);
[Link]("Press Enter Key to Exit..");
[Link]();
}
public static void Multiplication(int a)
{
a *= a;
[Link]("Variable Value Inside the Method: {0}", a);
}
}
}
Output :
Output:
10
10
⮚ In this example, it is not the value of n that is passed, rather a reference to n is
The parameter x is not an int. it is a reference to n.
⮚ Therefore, when x is equal to 10 inside the method, what actually gets x refers
to n.
⮚ The value of the parameter is changed after calling the method.
Output:
10
Protected Access is limited to within the class definition and any class that
inherits from the class.
Protected Access is limited to the current assembly and types derived from the
containing class. All members in the current project and all members
Internal in derived class can access the variables.
⮚ Public:
There are no restrictions on accessing public members.
Accessibility:
Can be accessed by objects of the class.
Can be accessed by derived class.
Example:
Class AccessMod
{
Public int num1;
}
Class program
{
Static void main()
{
AccessMod ob1=new AccessMod();
ob1.num1=100;
[Link](“Number is {0}”,ob1.num1);
[Link]();
}
}
Output:
Number is 100
⮚ Private:
Private members are accessible only within the body of the class or the struct in
which they are declared.
Accessibility:
● Connect can be accessed by object.
● Connect can be accessed by derived classes.
Example:
class AccessMod
{
public int num1;
int num2;
}
class program
{ static void main()
{ AccessMod ob1=new AccessMod();
Ob1.num1=100;
Ob1.num2=20; //Access to private members is not permitted.
}
}
Output:
Error: Compile error
⮚ Protected:
A protected member is accessible from within the class in which it is declared,
and from within any class derived from the class that declared this member.
Accessibility:
● Cannot be accessed by object.
● By derived class.
Example:
Previous examples like Inheritance.
⮚ Internal:
We can declare a class as internal or its members as internal. Internal
members are accessible only within files in the same assembly (.dll).
Accessibility:
In same assembly (public):
● Can be accessed by objects of the class.
● Can be accessed by derived class.
In other assembly:
● Cannot be accessed by object.
● Cannot be accessed by derived class.
⮚ Protected internal:
● The protected internal accessibility means protected or internal, not
protected and internal.
● A protected internal member is accessible from any class in the same
assembly, including derived class.
● Inherited types, they belong to a different assembly, have access to the
protected internal members.
● Types that reside in the same assembly, even if they are not derived from
the type, also have access to the protected internal members.