Sudhir c Sharp Book

download Sudhir c Sharp Book

of 76

Transcript of Sudhir c Sharp Book

  • 8/12/2019 Sudhir c Sharp Book

    1/76

    www.dbakings.com

    Copyrights Chekuri group

    Contents:

    Chapter 1

    Introduction to C#.NET

    Rules in C#.NET

    Datatypes

    Operators

    Creating first console application

    Basic C#.NET program structure in Console Application

    Console Methods

    Write

    WriteLine

    Read

    ReadLine

    Chapter 2

    Variables

    Predefined Methods

    Implicit Conversion

    Explicit Conversion

    Boxing

    UnBoxing

    Chapter 3

    Arrays

    Single dimension arrays

    Multidimensional arrays

    Jagged Array

    Chapter 4Conditional statements

    Branching

    Looping

    If

    If Else

  • 8/12/2019 Sudhir c Sharp Book

    2/76

    www.dbakings.com

    Copyrights Chekuri group

    Else if

    Nested if

    Switch

    Chapter 5

    for

    for each

    while

    do while

    Chapter 6

    OOPS

    methods

    General methods

    Methods with arguments

    Methods with return typeMethods with arguments and return type

    Static method

    Class

    Object

    Chapter 7

    Encapsulation

    Inheritance

    Single inheritance

    Multi level inheritance

    Hierachical inheritance

    Multiple inheritance

    Hybrid inheritance

    Sealed class

    Chapter 8

    Abstraction

    Access specifiers

  • 8/12/2019 Sudhir c Sharp Book

    3/76

    www.dbakings.com

    Copyrights Chekuri group

    Private

    Public

    ProtectedInternal

    Protected Internal

    Creating userdefined namespace or dll

    Chapter 9

    Polymorphism

    Method overloading

    Method overriding

    Abstract method

    Abstract class

    Partial class

    Chapter 10

    Interface

    Inheriting Multiple interfaces

    Delegates

    Single delegates

    Multcast delegates

    Exception handling

    Try

    Catch

    Finally

    Throw

  • 8/12/2019 Sudhir c Sharp Book

    4/76

    www.dbakings.com

    Copyrights Chekuri group

    Chapter 1

    Introduction to C#.NET

    C#.NET is an object oriented programming language and it isa part of .NET.

    It is used as a code behind language in DOTNET applications. It is the most popular language in .NET because it has the

    same syntax of programming languages like C, C++, JAVA.

    It is written as C# but pronounced as C Sharp because # is nottaken from maths it is a musical note which is pronounced asSharp.

    It is a case sensitive language i.e., Lowercase letter is not sameas Uppercase letter.

    Example

    int i; and int I; they are different variables.

    Every block of code starts with opening curly brace and endwith closing curly brace.

    opening brace {

    } closing brace

    All the keywords in C# will be in lowercase.Example

    int, using, class, ...

    Predefined namespaces, classes and methods will start withuppercase letter(Every word starts with uppercase letter).

    ExampleSystem, Console, Read

    Namespace is a collection of classes. Class is a collection of methods and its members.

    Rules in C#.NET

  • 8/12/2019 Sudhir c Sharp Book

    5/76

    www.dbakings.com

    Copyrights Chekuri group

    Every C# statement should end with semicolon (;).Example

    Console.Write("hello world"); Every string value should be surrounded by double quotes.

    Example

    string s="hai";

    Every character value should be surrounded by single quotes.Example

    char c='a';

    No need of any quotes around numeric values.Example

    int i=515; float f=533.558f;

    Every line of code in C# should be in class. // is for comments in C#

    Datatypes

    Datatypes and memory in bytes

    Byte 1

    Short 2

    Int 4

    Long 8

    Sbyte 1

    Ushort 2

    Uint 4

    Ulong 4

    Float 4

    Double 8

    Decimal 16

    Bool 1

    Char 2

    String 20+

    Object 8+

  • 8/12/2019 Sudhir c Sharp Book

    6/76

    www.dbakings.com

    Copyrights Chekuri group

    Example program with all datatypesusingSystem;

    usingSystem.Collections.Generic;

    usingSystem.Linq;

    usingSystem.Text;

    namespacedatatypes

    { classProgram

    {//main function is the starting point

    staticvoidMain(string[] args)

    {

    inti = 10;

    intj = 20;

    //int i = 234.234;

    //the text in double quotes will print as it is and value in ivariable is printed next to that.

    Console.WriteLine("int: "+i);//int: 10

    Console.WriteLine("sum: "+(i+j));//sum: 30

    floatf = 23.23f;floatf1 = 245;

    Console.WriteLine("float: "+(f+f1));//float: 268.23

    charc = 'a';

    // char c='abad';

    // char c = a;

    Console.WriteLine("char: "+c);//char: a

    strings = "abc";

    // string s = abc;

  • 8/12/2019 Sudhir c Sharp Book

    7/76

    www.dbakings.com

    Copyrights Chekuri group

    //string s='abd';

    strings1 = "def";

    Console.WriteLine(s+s1);//abcdef

    Console.WriteLine("string: "+s+s1);//string: abcdef

    //+ is used as concatenation(side by side) for strings

    //+ is used as addition operator for numeric datatypes

    Console.WriteLine(s+"\n"+s1);

    // \n is used to go to new line it should be in double quotes

    boolb = true;

    boolb1 = false;

    //bool b2 = "abc";

    Console.WriteLine(b);//true

    Console.WriteLine(b1);//false

    objecto = 123;

    objecto1 = 'a';

    objecto2 = "abc";

    objecto3 = 234.234f;

    objecto4 = true;

    Console.WriteLine("object: "+o3);

    Console.ReadKey();

    }}

    }

  • 8/12/2019 Sudhir c Sharp Book

    8/76

    www.dbakings.com

    Copyrights Chekuri group

    Operators

    Arithmetic operators:

    Used to work with numeric data types.

    eg: int , float, double, long, decimal, short...

    + (addition) eg: a+b

    - (substraction) eg: a-b

    * (multiplication) eg: a* b

    / (division) eg: a/b

    % (modulus) eg: a%b

    = equal to

    Assignment operators;

    +=

    eg: a+=b; ie., a=a+b;

    -=

    eg: a-=b; ie., a=a-b;

    *=

    eg: a*=b; ie., a=a*b;/=

    eg: a/=b; ie., a=a/b;

    %=

    eg: a%=b; ie., a=a%b;

  • 8/12/2019 Sudhir c Sharp Book

    9/76

    www.dbakings.com

    Copyrights Chekuri group

    Unary operator;

    ++ (increment)-- (decrement)

    i++(post increment)

    1 is added to i after the code is executed.

    ++i (pre increment)

    1 is added to i before the code gets executed.

    i--(post decrement)

    i is decreased by 1 after the code is executed.

    --i(pre decrement)

    i is decreased by 1 before the code gets executed.

    Example programusingSystem;

    usingSystem.Collections.Generic;

    usingSystem.Linq;

    usingSystem.Text;

    namespaceConsoleApplication1

    {

    classProgram

    {

    staticvoidMain(string[] args)

    {

    inti = 1;

    intj = i++;

    Console.WriteLine(j);//1

    Console.WriteLine(i);//2Console.WriteLine("----------------------");

    intx = 1;

    inty = ++x;

    Console.WriteLine(y);//2

  • 8/12/2019 Sudhir c Sharp Book

    10/76

    www.dbakings.com

    Copyrights Chekuri group

    Console.WriteLine(x);//2

    Console.ReadKey();

    }}

    }

    Relational operators:

    Used to compare two values in variables.

    Used in conditional statements.

    > greater than

    < less than

    >= greater than or equal to

  • 8/12/2019 Sudhir c Sharp Book

    11/76

    www.dbakings.com

    Copyrights Chekuri group

    eg: a=3>>4 ie., 3/2^4 =146

    Operator precedence:

    Based on the operator priority arthmetic statements are solved.

    bodmas

    brackets, division, multiplication, addition, substraction.

    Creating first console application

    Open visual studio. Click on file in menu. New project. Select a language as c# from the left side. Select console application from the list of applications. Give the name to the project Select the location to save the project. click on ok. Now default environment to create console application is

    displayed.

    F5 is the shortcut key to start debugging or to execute aprogram (or) click on play symbol button present in the center

    of top menu of visual studio tool.

    Shift + F5 for stop debugging or execution.

    Basic C#.NET program structure in Console Application

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace first

    {

    class Program

    {

  • 8/12/2019 Sudhir c Sharp Book

    12/76

    www.dbakings.com

    Copyrights Chekuri group

    static void Main(string[] args)

    {

    }}

    }

    Explanation:

    using- using is a keyword used to import a namespace to use the

    classes inside it in your coding.

    System- System is a namespace name which consists of classes used

    in coding.

    Like System we also have many namespaces given by Microsoft.

    By default we get the above four namespaces.

    first- first is the project name what you have given while creating this

    console application.

    Program- Program is the default class name for every console

    application.Main- Main is the method is acts as the starting point for every

    application.

    There should be only one main function for every project.

    Main is a static method which is created using static keyword and as it

    is a static method memory for main is created in ram when program is

    executed.

    void- void is the return type of Main method.

    Method with void return type returns nothing so main method also

    returns nothing.

    string[] args- Main method will take input arguments in the form of

    string array.

    We can also remove the namespaces like System.Text which are not

    used in the program.

    Console Methods

    Console is a class under System namespace.

    Under Console class we have 5 important methods.

    1. ReadKey

    2. Write

  • 8/12/2019 Sudhir c Sharp Book

    13/76

    www.dbakings.com

    Copyrights Chekuri group

    3. WriteLine

    4. Read

    5. ReadLine

    These five methods plays a prominent role in developing console

    applications.

    If System namespace is not added at the top of the program we have

    to use it everywhere where Console class is used.

    Example:

    Console.Write("System is added");

    System.Console.Write("System is not added");

    ReadKey

    ReadKey method is under Console class used to stop theoutput screen of a console application from closingimmediately after program execution till we press a key.

    So use Console.ReadKey( ) line at the end of Main function inevery program to see the output.

    It doesn't take any parameter between the braces.

    Example program to print a simple message using ReadKey:

    using System;

    namespace readkey

    {

    class Program

    {

    static void Main(string[] args)

    {Console.ReadKey();

    }

    }

    }

  • 8/12/2019 Sudhir c Sharp Book

    14/76

    www.dbakings.com

    Copyrights Chekuri group

    Output:

    _

    Explanation:

    Blank blank ouput screen waits without closing till you press a key this

    is because of ReadKey method.

    If that line is removed the output screen will close immediately after

    program execution completed.

    Write

    Write method is under Console class. It is used to print a message on the output screen of console

    application.

    When you print a message using Write the cursor will appearnext to the message.

    It prints the text as it is present in the double quotes. It also prints the value present in variable.

    Example program to print a simple message using Write:

    using System;

    namespace write

    {

    class Program

    {

    static void Main(string[] args)

    {

    Console.Write("Hai this is Write example");Console.ReadKey();

    }

    }

    }

  • 8/12/2019 Sudhir c Sharp Book

    15/76

    www.dbakings.com

    Copyrights Chekuri group

    Output:

    Hai this is Write example_

    Explanation:

    In the above program Write is a method under Console class used to

    print a message on the output screen with a cursor blinking next to

    message.

    As Console class is under System namespace we have to add it at the

    top.

    ReadKey method is used to make output screen without closing

    immediately.

    We can also write the above program without adding System

    namespace as below.

    Example program to print a simple message on using Write

    without adding System namespace:

    namespace write

    {

    class Program

    {

    static void Main(string[] args)

    {

    System.Console.Write("Hai this is Write example");

    System.Console.ReadKey();

    }

    }

    }

    Output:

    Hai this is Write example_

    Explanation:

  • 8/12/2019 Sudhir c Sharp Book

    16/76

  • 8/12/2019 Sudhir c Sharp Book

    17/76

    www.dbakings.com

    Copyrights Chekuri group

    Read

    Read method is under Console class used to Read data fromthe user given in runtime.

    When you read a message using Read the cursor willappear in the same line.

    Return type of Read method is int ie., it returns back the datagiven by the user in the form of int.

    It returns ascii value of the given data. It doesn't take any parameter between the braces.

    Example:

    int i=Console.Read();Console.Write(i);

    if the user enters a its ascii value 97 is stored in i.

    Here, Console.Write method is used to print the value inside i, So, the

    output will be 97.

    Example program to print a simple message using Read:

    using System;

    namespace read

    {

    class Program

    {

    static void Main(string[] args)

    {

    int i=Console.Read();Console.Write("\n");

    Console.Write("you entered: "+i);

    Console.ReadKey();

    }

    }

  • 8/12/2019 Sudhir c Sharp Book

    18/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    Output:

    a

    you entered: 97_

    Explanation:

    In the above program i is a variable of int datatype which is used to

    store the ascii value of the data given by user in runtime using Read

    method. First Write method is used for new line and second Write

    method is used to print message "you entered: " as it is and also to

    concatenate the value present in the variable i as show in the output.

    ReadLine

    ReadLine method is under Console class used to Read datafrom the user given in runtime.

    When you read a message using ReadLine the cursor willappear in the next line.

    Return type of ReadLine method is string ie., it returns backthe data given by the user in the form of string.

    It returns the value as it is given by the user so ReadLinemethod is preferred to take input data from the user in

    runtime.

    It doesn't take any parameter between the braces.

    Example:

    string s=Console.ReadLine();Console.Write(s);

    if the user enters a then a as it is stored in string variable s.

    Here, Console.Write method is used to print the value inside s, So, the

    output will be a.

  • 8/12/2019 Sudhir c Sharp Book

    19/76

    www.dbakings.com

    Copyrights Chekuri group

    Example program to print a simple message using ReadLine:

    using System;namespace readline

    {

    class Program

    {

    static void Main(string[] args)

    {

    string s=Console.ReadLine();

    Console.Write("you entered: "+s);

    Console.ReadKey();

    }

    }

    }

    Output:

    a

    you entered: a_

    Explanation:

    In the above program s is a variable of string datatype which is used

    to store the value given by user in runtime using ReadLine method.

    Write method is used to print message "you entered: " as it is and

    also to concatenate the value present in the variable s as show in the

    output.

    Chapter 2

    Variables

    Variable is used to store a value of a specific datatype. Memory for a variable is allocated in stack memory. The value is a variables keeps on changing throughout the

    program.

  • 8/12/2019 Sudhir c Sharp Book

    20/76

    www.dbakings.com

    Copyrights Chekuri group

    Example:

    int i;

    i=10;Here i is a variable of type int.

    So, the value between -2,147,483,648 to 2,147,483,648 can be stored

    in variable i.

    In the second statement value 10 is stored in i.

    We can also intialize the variable with some value when it is created

    like below.

    Example program to create a variable of int type, initialization

    and also assigning value to it:

    using System;

    namespace variable

    {

    class Program

    {static void Main(string[] args)

    {

    int i=10;

    i = 20;

    Console.Write(i);

    Console.ReadKey();

    }

    }

    }

    Output:

    20_

    Explanation:

    In the above program i is a variable of int datatype. It is intialized witha value of 10. In the next statement i is assigned with a new value 20.

    So, the value in the variable is changed from 10 to 20. When we print

    the value in variable i using WriteLine the output is 20.

  • 8/12/2019 Sudhir c Sharp Book

    21/76

    www.dbakings.com

    Copyrights Chekuri group

    In the same way we can create any number of variables in a program

    with specific datatypes and based on the datatypes values should be

    stored in variables.

    Example program for creating variables with different datatypes

    and printing values in them:

    using System;

    namespace variable

    {

    class Program

    {

    static void Main(string[] args)

    {

    int i = 20; float f = 23.234f; char c = 'b'; string s = "def"; bool b

    = false;

    Console.WriteLine(i+f);

    Console.WriteLine(c+s);Console.WriteLine(i+c);

    Console.WriteLine(i+s);

    Console.WriteLine(b);

    Console.ReadKey();

    }

    }

    }

    Output:

    43.234

    bdef

    118

    20def

    false

    _

    Explanation:

    There are 4 variables i, f, c, s, b with datatypes int, float, char, string,

    bool respecitively.

  • 8/12/2019 Sudhir c Sharp Book

    22/76

    www.dbakings.com

    Copyrights Chekuri group

    int and float is of numeric datatypes so variables i and f stores

    numbers and there is no need of having quotes around the values.

    f is of float type so it can store numeric value with decimal point butwhen f should be the suffix for the value which is assigned to f.

    c variable is of char type and it is capable of storing only one character

    at a time whereas variable s can store multiple characters as it is of

    string datatype.

    b variable is capable of storing either true or false value only.

    i + f = 20 + 23.234 = 43.234

    As i and f are numeric types + is used as addition arthimetic operator

    and they are added.

    c + s= b + def = bdef

    As c is char and s is string + is used as concatenation operator and

    they are concatenated.

    i + c = 20 + b = 20 + 98 = 118

    As i is integer and c is char the ascii value of c is added to i ie., ascii of

    b is 98 added to 20 gives output as 118.

    i + s = 20 + def = 20defAs s is string it cannot be converted into ascii value so here + is used

    for concatenating values of i and s.

    b = false

    Value inside b is false and it is printed as it is.

    Predefined methods

    Predefined methods are the inbuilt methods we have in C#.NET which

    do something or returns a value.

    We have predefined methods like

    minvalue( ):

    min value is a method used to find out the minimum value a numeric

    datatype can store.

    It is only for numeric types.

    maxvalue( ):

    max value is a method used to find out the maximum value a numeric

    datatype can store.

  • 8/12/2019 Sudhir c Sharp Book

    23/76

    www.dbakings.com

    Copyrights Chekuri group

    It is only for numeric types.

    Example program to print minvalue and maxvalue of numericdatatypes int, uint, long, float, double, decimal

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace min_max_value

    {

    class Program

    {

    static void Main(string[] args)

    {

    Console.WriteLine(int.MinValue);

    Console.WriteLine(int.MaxValue);Console.WriteLine(uint.MinValue);

    Console.WriteLine(uint.MaxValue);

    Console.WriteLine(long.MinValue);

    Console.WriteLine(long.MaxValue);

    Console.WriteLine(float.MinValue);

    Console.WriteLine(float.MaxValue);

    Console.WriteLine(double.MinValue);

    Console.WriteLine(double.MaxValue);

    Console.WriteLine(decimal.MinValue);

    Console.WriteLine(decimal.MaxValue);

    Console.ReadKey();

    }

    }

    }

    output:

  • 8/12/2019 Sudhir c Sharp Book

    24/76

    www.dbakings.com

    Copyrights Chekuri group

    Example program to print even numbers between 10 and 45 using

    for loop and if condition:

    using System;

    namespace variable

    {

    class Program

    {

    static void Main(string[] args)

    {

    for (int i = 10; i < 45; i++)

    {

    if (i % 2 == 0)

    { Console.WriteLine(i); }

    }

    Console.ReadKey();}

    }

    }

    Output:

    10

    12

    14

    16

    18

    20

    22

    24

    26

    2830

    32

    34

    36

    38

  • 8/12/2019 Sudhir c Sharp Book

    25/76

    www.dbakings.com

    Copyrights Chekuri group

    40

    42

    44

    Implicit Conversion

    Converting small data type values to large data type values

    automatically by the compileris known as "Implicit Conversion".

    Example:

    int i=10;

    long l=i;

    Console.Write(l);//10

    Explanation:

    int data type is smaller than long data type ie., int data type occupies

    small range as compare to long data type.so conversion of int to longis possible.it is an example of implicit conversion.

    Explicit conversion

    Converting large data type values to small data type values explicitly

    by the developer is known as "Explicit Conversion".

    Example:

    long l=10;

    int i=Convert.ToInt32(l);

    Console.Write(i);//10

    Explanation:

    long data type is longer than int data type ie.,long occupies long

    range as compare to int.so conversion of long to int is difficult to thedeveloper.so developer use the method Convert.ToInt32() to convert

    long values into int values.Then conversion is possible and easy.

    Boxing

  • 8/12/2019 Sudhir c Sharp Book

    26/76

    www.dbakings.com

    Copyrights Chekuri group

    Converting value type to reference type is known as "Boxing".

    Example:

    int i=10;object o=i;

    Console.WriteLine(o);//10

    Explanation:

    In the above example int datatype is value type.object is reference

    type.so conversion of int to object is possible and it is an example

    of boxing.

    UnBoxing

    Converting reference type to value type type is known as "UnBoxing".

    Example:

    object o=10;

    int i=Convert.ToInt32(o);

    Console.WriteLine(i);//10

    Explanation:

    In the above example object is reference type.int is value

    type.conversion of object to int is not possible.so here we use

    Convert.ToInt32() to convert object to int.

    Chapter 3

    Arrays

    Array is a type used to store multiple values of same data typeunder a single name.

    Arrays allows to store multiple values but all the values shouldbe same data type.

    The elements can't exceed the number while creating an array. Arrays are zero indexed that means index starts with zero.

  • 8/12/2019 Sudhir c Sharp Book

    27/76

    www.dbakings.com

    Copyrights Chekuri group

    Arrays are objects in c#.Memory objects are created in heapmemory.

    Index of an array will be used to retrieve values. Array.Reverse()is used to reverse the elements of an array. Array.Sort() is used to print values in ascending order. Array.Length() is used to get the no.of elements in the array.

    Single Dimensional Array

    Single Dimensional Array is used to store multiple values in a linear

    fashion.

    Syntax for creating a single dimensional array:

    datatype[ ] arrayname=new datatype[no. of elements];

    Example:

    int[ ] i=new int[3];

    Example program for creating single dimensional array using for

    loop:

    using System;using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace sdarray

    {

    class Program

    {static void Main(string[] args)

    {

    //int[] i = new int[3];

    // i[0] = 1;------Assign a value

    // i[1] = 2;------Assign a value

  • 8/12/2019 Sudhir c Sharp Book

    28/76

    www.dbakings.com

    Copyrights Chekuri group

    // i[2] = 3;------Assign a value

    int[] i = new int[3] { 1, 2, 3 };

    for (int a = 0; a < 3; a++)

    {

    Console.WriteLine(i[a]);

    }

    Console.ReadKey();

    }

    }

    }

    Output:

    1

    2

    3

    Example program for creating single dimensional array using

    foreach loop:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace sdarray

    {

    class Program

    {

    static void Main(string[] args)

    {

    int[] i = new int[3] { 3,4,5 };foreach (int a in i)

    {

    Console.WriteLine(a);

    }

    Console.ReadKey();

  • 8/12/2019 Sudhir c Sharp Book

    29/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    }

    }

    Output:

    3

    4

    5

    Multi Dimensional Array

    Multi Dimensional Array is used to store elements in the form of a

    table.

    Syntax for creating a multi dimensional array:

    datatype[ , ] arrayname=new datatype[row,col];

    Example:

    int[ ,] a=new int[2,3];

    Example program for creating multi dimensional array using for

    loop:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace sdarray

    {class Program

    {

    static void Main(string[] args)

    {

    int[ , ] a = new int[2,3]{{5,6,3}, { 3,4,5} };

  • 8/12/2019 Sudhir c Sharp Book

    30/76

  • 8/12/2019 Sudhir c Sharp Book

    31/76

    www.dbakings.com

    Copyrights Chekuri group

    {

    class Program

    {static void Main(string[] args)

    {

    int[][] a = new int[2][];

    a[0] = new int[3] { 3, 4, 5 };

    a[1] = new int[4] { 1, 2, 3, 4 };

    for (int i = 0; i < 2; i++)

    {

    for (int j = 0; j < a[i].Length; j++)

    {

    Console.Write(a[i][j]);

    }

    Console.WriteLine("");

    }

    Console.ReadKey();}

    }

    }

    Output:

    345

    1234

    Chapter4

    Conditional Statements

    A block of code that executes basing upon a condition is "ConditionalStatement".Conditional statements are used to execute a block of

    code only when the condition is satisfied.

    Conditional statements are classified into two types.

    1)Conditional Branching

    2)Conditional Looping

  • 8/12/2019 Sudhir c Sharp Book

    32/76

    www.dbakings.com

    Copyrights Chekuri group

    Conditional Branching

    These statements allows you to branch code depending on whether

    the certain conditions met or not.

    C# has two constructs for branching code.

    1)If Statement

    2)Switch Statement

    Conditional Looping

    C# provides four different loops that allows you to execute a block of

    code repeatedly until a certain condition is met.

    Those are:

    1) for loop

    2) while loop3) dowhile loop

    4) foreach loop

    If Statement:

    The if statement which allows you to test whether a specific conditionis met.

    Syntax of If Condition:

    if(condition)

    {

    //statements

    }

    Example program using if condition

    using System;

    using System.Collections.Generic;

    using System.Linq;

  • 8/12/2019 Sudhir c Sharp Book

    33/76

    www.dbakings.com

    Copyrights Chekuri group

    using System.Text;

    namespace branching{

    class Program

    {

    static void Main(string[] args)

    {

    int i;

    Console.WriteLine("Enter number");

    i = Convert.ToInt32(Console.ReadLine());

    if (i > 0)

    {

    Console.WriteLine("Positive");

    }

    Console.ReadKey();

    }

    }}

    Output:

    Enter number

    12

    Positive

    Output-2:

    Enter number

    -5

    _

    Explanation:

    In the above output we can give input is -5.here -5 is less than zero.so

    nothing will be printed in output. Blank will be printed in output.

    If else statement:

  • 8/12/2019 Sudhir c Sharp Book

    34/76

    www.dbakings.com

    Copyrights Chekuri group

    It is one of the statement in conditional branching. It is used to check

    whether the condition is true or false. if it is true, it goes to if condition

    and print output. if it is false, it goes to else part of the program andprint the output.

    Syntax for if else statement:

    if(condition)

    {

    statement

    }

    else

    {

    statement

    }

    Example program using if else statement

    using System;using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ifelse

    {

    class Program

    {

    static void Main(string[] args)

    {

    int i;

    Console.WriteLine("Enter number");

    i = Convert.ToInt32(Console.ReadLine());

    if (i > 0)

    {Console.WriteLine("Positive");

    }

    else

    {

    Console.WriteLine("Negative");

  • 8/12/2019 Sudhir c Sharp Book

    35/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    Console.ReadKey();

    }}

    }

    Output:

    Enter number

    12

    Positive

    Output-1:

    Enter number

    -6

    Negative

    Explanation:

    In the above example we can give input is 12 then the output will beprinted as positive because 12 is greaterthan zero.we can give input is

    -6 then the output will be printed as negative because -6 is less than

    zero.

    Else if statement

    This is one type of conditional branching.In this type the compiler 1st

    goes to if condition,if it is false it goes to else if condition,it also false

    finally goes to else statement and print output.

    Syntax for Else if statement:

    if(condition)

    {statements

    }

    else if(condition)

    {

    statements

  • 8/12/2019 Sudhir c Sharp Book

    36/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    else

    {statements

    }

    Example program using Else if statement

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ifelse

    {

    class Program

    {

    static void Main(string[] args){

    int i;

    Console.WriteLine("Enter number");

    i = Convert.ToInt32(Console.ReadLine());

    if (i > 0)

    {

    Console.WriteLine("Positive");

    }

    else if(i

  • 8/12/2019 Sudhir c Sharp Book

    37/76

    www.dbakings.com

    Copyrights Chekuri group

    Output:

    Enter number223

    Positive

    Output-1:

    Enter number

    -741

    Negative

    Output-2:

    Enter number

    0

    zero

    Explanation:

    In the above example we give input is 223,it is greater than zero,thenthe output will be printed as positive and we give input is -741 it is

    less than zero,then the compiler goes to else if part of the program

    and print output as negative and finally gives 0 it is equal to zero,then

    the compiler goes to else pert of the program and print output as

    zero.

    Nested If Statement

    It is one type of If statement.Nested If means if in if ie.,The if

    statement contains another if within that.

    Syntax for Nested If:

    if(condition)

    {if(condition)

    {

    if(condition)

    {

    statements

  • 8/12/2019 Sudhir c Sharp Book

    38/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    }

    }

    Example program for Nested If Statements

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ifelse

    {

    class Program

    {

    static void Main(string[] args)

    {

    int i;

    Console.WriteLine("Enter number");i = Convert.ToInt32(Console.ReadLine());

    if (i > 0)

    {

    if (i > 5 && i < 10)

    {

    Console.WriteLine("Between 5 and 10");

    }

    }

    Console.ReadKey();

    }

    }

    }

    Output:

    Enter number

    4_

    Output-1:

    Enter number

    8

  • 8/12/2019 Sudhir c Sharp Book

    39/76

  • 8/12/2019 Sudhir c Sharp Book

    40/76

  • 8/12/2019 Sudhir c Sharp Book

    41/76

    www.dbakings.com

    Copyrights Chekuri group

    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.

    Another Example program for switch statement:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace branching

    {

    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");

  • 8/12/2019 Sudhir c Sharp Book

    42/76

    www.dbakings.com

    Copyrights Chekuri group

    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.

    Chapter5

    1)for loop:

    for loop is used to execute a block of 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, forloop 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: The code is executed until the condition satisfied ie.,until itreturns 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

  • 8/12/2019 Sudhir c Sharp Book

    43/76

    www.dbakings.com

    Copyrights Chekuri group

    direction. Some times it will be incremented, sometimes decremented

    depending on the situation.

    Syntax for for loop:

    for(initialize; condition; inc/dec)

    {

    statements

    }

    Example program using for loop

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    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:

    *

  • 8/12/2019 Sudhir c Sharp Book

    44/76

    www.dbakings.com

    Copyrights Chekuri group

    **

    ***

    ****

    2)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 foreach loop:

    foreach(type variable in collect:ion/array)

    {

    statements

    }

    Example program using foreach loop

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    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();

  • 8/12/2019 Sudhir c Sharp Book

    45/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    }

    }

    Output:

    7

    8

    9

    3)While loop

    whileloop will check a condition and then continues to execute a

    block of code as long as the condition is true.

    Syntax for While loop:

    while(condition){

    statements

    }

    Example program to print numbers from 0 to 5 using while loop

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace loops

    {

    class Program

    {

    static void Main(string[] args){

    int i=0;

    while (i

  • 8/12/2019 Sudhir c Sharp Book

    46/76

    www.dbakings.com

    Copyrights Chekuri group

    i++;

    }

    Console.ReadKey();}

    }

    }

    Output:

    0

    1

    2

    3

    4

    5

    4)Dowhile loop

    Dowhile loop is similar to the whileloop, except that it checks its

    condition at the end of the loop. This means that the dowhileloop is

    guaranteed to execute at least one time even the condition fails.

    Syntax for dowhile loop:

    do

    {

    statements

    }

    while(condition);

    Example program to print numbers from 0 to 5 using dowhile

    loop

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

  • 8/12/2019 Sudhir c Sharp Book

    47/76

    www.dbakings.com

    Copyrights Chekuri group

    namespace loops

    {

    class Program{

    static void Main(string[] args)

    {

    int i=0;

    do

    {

    Console.WriteLine(i);

    i++;

    }

    while(i

  • 8/12/2019 Sudhir c Sharp Book

    48/76

    www.dbakings.com

    Copyrights Chekuri group

    Chapter6

    OOPS

    OOPS stands for Object Oriented Programming Structures.

    It was a new approach in programming introduced in 1970's

    Object Oriented approach provides us security and reusability of the

    code.

    To call a language as Object Oriented it needs to satisfy the below

    oops concepts listed below

    Encapsulation

    Abstraction

    Inheritance

    Polymorphism

    Before going to the discussion about oops concepts we have learn

    what a method, object and a class is. They are the basic building

    blocks in oops concepts.

    Methods

    Method is a named block of code or piece of code used toperform a particular task.

    Methods may or may not return a value.

    If it was a non value returning we use void as the return typefor a method.

    If it was a value returning we use type of value as its returntype.

    Syntax for creating a method:

    Access_specifier return_type methodname( )

    {//code

    }

    Access Specifier:It is used to define the accessibility level of a

    method ie.,how long it is going to call.

  • 8/12/2019 Sudhir c Sharp Book

    49/76

    www.dbakings.com

    Copyrights Chekuri group

    ex of access specifiers are public, private, protected etc.

    ReturnType:What type of value a method is returning.

    Method name: The name of a method to call it.

    Example:

    public void add( )

    { }

    There are different types of methods

    Simple Method

    Method with arguments

    Method with return type

    Method with arguments and return type

    Abstract Method

    Static Method

    Simple Method

    Simple method is a method that doesn't have any arguments and

    return type.

    Syntax:

    Access specifier void methodname( )

    { }

    Example:

    public void add()

    {

    Console.WriteLine("addition");}

    The above method add will print addition as message on the ouput

    screen. This is a simple method which taken no input and doesnt

    return anything.

  • 8/12/2019 Sudhir c Sharp Book

    50/76

    www.dbakings.com

    Copyrights Chekuri group

    Method with arguments

    It is one type of method and consists of arguments/parameters.Arguments can be passed to a method which are inputs to the

    method. The value passed to the method as arguments can be used in

    the code of the method.

    Example:

    public void add(int a,int b)

    {

    Console.WriteLine(a+b);

    }

    Explanation:

    In the above example the add( ) method can take two values of int

    type as inputs and doesn't return any type because void is its returntype.

    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()

    {

    int i=10;

    int j=20;

    return i+j;}

    Explanation:

    The above method add will return a value of int datatype.

  • 8/12/2019 Sudhir c Sharp Book

    51/76

    www.dbakings.com

    Copyrights Chekuri group

    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;

    }

    Explanation:

    The above method add can take two parameters of int type and

    returns a value of long datatype.

    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);

    Explanation:

  • 8/12/2019 Sudhir c Sharp Book

    52/76

    www.dbakings.com

    Copyrights Chekuri group

    The above method add is an abstract method which is created using

    abstract keyword at the starting of the method signature. Signature

    method means the whole line present there which defines a method.It accepts two parameters and returns a value of long datatype. It has

    no body or implementation code.

    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 using static method and takes input

    arguments with return type

    using System;using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    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;

    }

    }

  • 8/12/2019 Sudhir c Sharp Book

    53/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    }

    Output:

    11

    Explanation:

    add is a method created in maths class. The method add is called in

    the main method directly using dot after class name as add is a static

    class otherwise it has to be called using object of maths class.

    class

    Class is a blue print. It is a collection of methods and member functions. In C#.NET every line of code should be in class. Class can contain variable declarations as well as initialize

    them at the time of declaration only.

    Syntax:

    class classname{

    body

    }

    Example program using Class:

    using System;

    using System.Collections.Generic;

    using System.Linq;using System.Text;

    namespace static_method

    {

    class Program

  • 8/12/2019 Sudhir c Sharp Book

    54/76

    www.dbakings.com

    Copyrights Chekuri group

    {

    static void Main(string[] args)

    {maths m = new maths();

    m.add();

    Console.ReadKey();

    }

    class maths

    {

    public void add()

    {

    int a = 10;

    int b = 20;

    Console.WriteLine("Addition is:"+(a + b));

    }

    }

    }

    }

    Output:

    Addition is: 30

    Object

    Object is an instance of class. Object will have memory. To create this object "new" keyword is used.

    It is a reference type.

    Mandatory to use the "new" keyword for creating object.

    Syntax:

    classname objectname= new constructor;

  • 8/12/2019 Sudhir c Sharp Book

    55/76

    www.dbakings.com

    Copyrights Chekuri group

    Example:

    maths m=new maths( );

    Explanation:

    M is the name of the object created for maths class using new

    keyword. Maths( ) is the constructor of the class maths.

    Constructor will be explained later.

    Example program using object for a class:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace static_method{

    class Program

    {

    static void Main(string[] args)

    {

    maths m = new maths();

    long l = m.add(3, 5);

    Console.WriteLine("Sum is:"+l);

    Console.ReadKey();

    }

    class maths

    {

    public long add(int i,int j)

    {

    return i + j;}

    }

    }

    }

  • 8/12/2019 Sudhir c Sharp Book

    56/76

    www.dbakings.com

    Copyrights Chekuri group

    Output:

    sum is : 8

    Chapter 7

    Encapsulation

    Encapsulation is a procedure of binding data under classname .

    Encapsulation is a procedure of covering up of the data &functions into a single unit called as Class.

    An encapsulated object is often called an abstract data type. Rather than defining the data in the form of public,we can

    declare those fields as private. In other words wrapping up of data and methods into a single

    unit is called as "Encapsulation"(ex:Class,method).

    Inheritance

    Acquiring or getting the properties from one class to anotherclass is known as "Inheritance".

    In Object Oriented approach if we can apply parent/childrelation between classes.

    Members of the parent class can be consumed by the childclasses.

    Syntax:

    class parent

    {

    body

    }

    class child : parent

    {

    body

  • 8/12/2019 Sudhir c Sharp Book

    57/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    Example program:Two classes a & b with 2 methods display & show inside them with

    inheritance

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace Inheritance

    {

    class Program

    {

    static void Main(string[] args)

    {

    b o1 = new b();//creating object for the derived class

    o1.show();//calling method from child classo1.display();//calling method from parent class

    Console.ReadKey();

    }

    class a//parent or base class

    {

    public void display()

    {

    Console.WriteLine("display");

    }

    }

    class b : a//child or derived class inherited with base class

    {

    public void show()

    {

    Console.WriteLine("show");}

    }

    }

    }

  • 8/12/2019 Sudhir c Sharp Book

    58/76

    www.dbakings.com

    Copyrights Chekuri group

    Output:

    show

    display

    Single Inheritance

    When a single derived class is derived from single baseclass,then that inheritance is called as "single inheritance".

    In single inheritance we have single base class that is inheritedby the derived class.

    Here the derived class has all the features of the base classand can add some new features.

    The inheritance also depends on the access specifier that isused at the time of base class inheritance.

    Syntax:

    class Parent{

    body

    }

    class child : parent

    {

    body

    }

    Example :

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace Inheritance{

    class Program

    {

    static void Main(string[] args)

    {

  • 8/12/2019 Sudhir c Sharp Book

    59/76

    www.dbakings.com

    Copyrights Chekuri group

    dad d = new dad();

    d.car();

    son s = new son();s.bike();

    s.car();

    Console.ReadKey();

    }

    class dad

    {

    public void car()

    {

    Console.WriteLine("car");

    }

    }

    class son:dad

    {

    public void bike()

    {Console.WriteLine("bike");

    }

    }

    }

    }

    Output:

    car

    bike

    car

    Multi level Inheritance

    Acquiring properties to a subchild class from the child classand also from the parent class.

    When a derived class is created from another derivedclass,then that inheritance is called as "Multi level Inheritance".

  • 8/12/2019 Sudhir c Sharp Book

    60/76

  • 8/12/2019 Sudhir c Sharp Book

    61/76

  • 8/12/2019 Sudhir c Sharp Book

    62/76

    www.dbakings.com

    Copyrights Chekuri group

    Syntax:

    class parent{

    body

    }

    class child1 : parent

    {

    body

    }

    class child2 : parent

    {

    body

    }

    Example:

    using System;

    using System.Collections.Generic;using System.Linq;

    using System.Text;

    namespace Inheritance

    {

    class Program

    {

    static void Main(string[] args)

    {

    grandfather g=new grandfather();

    g.helicopter();

    dad d = new dad();

    d.helicopter();

    d.car();

    son s = new son();

    s.helicopter();s.bike();

    Console.ReadKey();

    }

    class grandfather

    {

  • 8/12/2019 Sudhir c Sharp Book

    63/76

    www.dbakings.com

    Copyrights Chekuri group

    public void helicopter()

    {

    Console.WriteLine("helicopter");}

    }

    class dad:grandfather

    {

    public void car()

    {

    Console.WriteLine("car");

    }

    }

    class son : dad

    {

    public void bike()

    {

    Console.WriteLine("bike");

    }}

    }

    }

    Output:

    helicopter

    helicopter

    car

    helicopter

    bike

    Multiple Inheritance

    One child with two parents is known as "Multiple Inheritance". Multiple Inheritance can contain only one derived class,two or

    more base classes.

    When a derived class is created from more than one baseclass then that inheritance is called as"Multiple Inheritance".

  • 8/12/2019 Sudhir c Sharp Book

    64/76

    www.dbakings.com

    Copyrights Chekuri group

    Multiple Inheritance is not supported in c# .net because dueto ambiguity or confusion problem.

    Syntax:

    class Parent

    {

    body

    }

    class parent2

    {

    body

    }

    class child : parent,parent2

    Example:

    using System;

    using System.Collections.Generic;

    using System.Linq;using System.Text;

    namespace Inheritance

    {

    class Program

    {

    static void Main(string[] args)

    {

    grandfather g = new grandfather();

    g.helicopter();

    dad d = new dad();

    d.car();

    Console.ReadKey();

    }

    class grandfather{

    public void helicopter()

    {

    Console.WriteLine("helicopter");

    }

  • 8/12/2019 Sudhir c Sharp Book

    65/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    class dad

    {public void car()

    {

    Console.WriteLine("car");

    }

    }

    class son : dad, grandfather

    {

    public void bike()

    {

    Console.WriteLine("bike");

    }

    }

    }

    }

    Output:

    An error will occur because a class cannot have multiple base classes

    Hybrid Inheritance

    Any combination of single, hierarchial and multi level inheritance is

    called as "Hybrid Inheritance".

    Syntax:

    class base class

    {

    body

    }

    class derived class : base class{

    body

    }

    class derived class1 : base class/derived class

    {

  • 8/12/2019 Sudhir c Sharp Book

    66/76

    www.dbakings.com

    Copyrights Chekuri group

    body

    }

    Example:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace Inheritance

    {

    class Program

    {

    static void Main(string[] args)

    {

    grandfather g = new grandfather();

    g.helicopter();

    dad d = new dad();d.helicopter();

    d.car();

    son s = new son();

    s.helicopter();

    s.bike();

    s.car();

    Console.ReadKey();

    }

    class grandfather

    {

    public void helicopter()

    {

    Console.WriteLine("helicopter");

    }

    }class dad:grandfather

    {

    public void car()

    {

    Console.WriteLine("car");

  • 8/12/2019 Sudhir c Sharp Book

    67/76

    www.dbakings.com

    Copyrights Chekuri group

    }

    }

    class son : dad{

    public void bike()

    {

    Console.WriteLine("bike");

    }

    }

    }

    }

    Output:

    helicopter

    helicopter

    car

    helicopterbike

    car

    Sealed Class

    A class which cannot be inherited by any other class is knownas "Sealed class".

    It should be declared using "sealed" modifier. sealed classes are used to restrict the inheritance feature of

    object oriented programming.

    once a base class is defined as "sealedclass",this class cannotbe inherited.

    In c#,the sealed modifier is used to define a class as "sealed". If a class is derived from a sealed class,compiler throws an

    error.

    Syntax:

    sealed class classname

    {

  • 8/12/2019 Sudhir c Sharp Book

    68/76

    www.dbakings.com

    Copyrights Chekuri group

    body

    }

    class classname2:classname//Invalid because derived class will notinherit sealed class.

    Example:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace sealed

    {

    class Program

    namespace sealed_class

    {

    class Program{

    static void Main(string[] args)

    {

    SealedClass sealedCls = new SealedClass();

    int total = sealedCls.Add(4, 5);

    Console.WriteLine("Total = " + total.ToString());

    Console.ReadKey();

    }

    }

    // Sealed class

    sealed class SealedClass

    {

    public int Add(int x, int y)

    {return x + y;

    }

    }

    }

  • 8/12/2019 Sudhir c Sharp Book

    69/76

    www.dbakings.com

    Copyrights Chekuri group

    Output:

    Total = 9

    Chapter 8

    Abstraction

    Hiding the complexity and providing the services is called"Abstraction".

    In other words hiding the unnecessary data and providingrequired facilities is called "Abstraction".

    ex: System.Console.ReadKey(); ReadKey() is used without writing lots of code in it. Another example is Tv remote.we can change the channels

    but we don't know how the inner button works.

    Access Specifiers

    Access specifiers are also called as modifiers used to define scope of a

    type as well as its members ie.,who can access it and who can't access

    it.

    C# supports five different access specifiers.Those are:

    1)Private

    2)Public

    3)Protected4)Internal

    5)Protected Internal

    Private

    Private can be used "Only with in the block". Members declared as private with in class or structure aren't

    possible outside of them in which they are defined.

    In C# the default scope for members of a class or structure is"Private".

    Where as in case of interface default is "Public".Public

  • 8/12/2019 Sudhir c Sharp Book

    70/76

    www.dbakings.com

    Copyrights Chekuri group

    A type or member of a type is declared as Public,that can be accessed

    from "Anywhere".

    Protected

    Members declared as Protected under a class can be accessedonly from child classes.

    Protected cannot consume non-child classes. Protected can be used "Only inherited class". Types under a namespace can't be declared as a Protected.

    Internal

    Internal can be used "Only with in the project". Members which are declared as internal were accessible only

    with in the project both from child or non-child classes.

    The default scope for a type in C# is "Internal"only.Protected Internal

    Members declared as "Protected Internal" enjoy dual scopeie.,Internal and Protected.

    With in the project they behave as "internal",providing accessto all other classes.

    Outside the project they change to "Protected", and stillprovide access to child classes.Creating user defined dll or namespace

    dll stands for "Dynamic Link Library".

    It is an assembly.

    It is a supportable file.

    It is also known as "namespace",which is a collection of classes.

    Namespaces are used in .exe files by adding them as references.

    Namespaces are imported using the keyword "using".

    Steps to create dll or namespace:

  • 8/12/2019 Sudhir c Sharp Book

    71/76

  • 8/12/2019 Sudhir c Sharp Book

    72/76

    www.dbakings.com

    Copyrights Chekuri group

    Overloading".

    2) Dynamic Polymorphism:

    It is also known as "Run time polymorphism". It can have same

    method name and same signature. It is also known as "Method

    Overriding". Method Overriding can be used only when we have

    inheritance.

    Example:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace polymorphism

    {class Program

    {

    static void Main(string[ ] args)

    {

    y o = new y();

    o.add(1, 3);

    Console.ReadKey();

    }

    }

    class x

    {

    public void add(int a, int b)

    {

    Console.WriteLine(a + b);

    }}

    class y : x

    {

    public void add(int a, int b)

    {

  • 8/12/2019 Sudhir c Sharp Book

    73/76

    www.dbakings.com

    Copyrights Chekuri group

    Console.WriteLine(a - b);

    }

    }

    }

    Output:

    -2

    Method Overloading

    Method overloading is an approach which allows to definemultiple methods with the same name and different

    signatures.

    Method overloading can be done within a class as well asunder the child classes also.

    Method overloading doesn't require any permissions tooverload a method from parent to child classes.

    Method Overriding

    Method overriding is an approach which allows to definemultiple methods with same name and same signatures.

    Method overriding can be done only under child classes.

    Method overloading requires an explicit permission tooverride a method from parent to child classes.

    It can be done only we have inheritance.Abstract Method

    Abstract method is a method which is created using "abstract"keyword.

    Abstract method doesn't contain any implementation or body. In abstract method we have semicolon(;)at the end . Abstract method doesn't have curly braces. We can create abstract method only in abstract class.

  • 8/12/2019 Sudhir c Sharp Book

    74/76

  • 8/12/2019 Sudhir c Sharp Book

    75/76

    www.dbakings.com

    Copyrights Chekuri group

    {

    Program p = new Program();

    p.add();p.sub();

    Console.ReadKey();

    }

    //implementing abstract method in abstract class

    public override void add()

    {

    Console.WriteLine("addition");

    }

    }

    abstract class a

    {

    //abstract method

    abstract public void add();

    //genaral method

    public void sub(){

    Console.WriteLine("subtraction");

    }

    }

    }

    Output:

    addition

    subtraction

    Partial Class

    Partial class is a class which can be defined a single class inmultiple files,when compilation time these classes are

    combined to form a single class. Partial classes can be defined with the keyword "partial". All the Partial classes must have the same accessibility. If any part is declared as "sealed",the entire class is sealed. If any part is declared as "abstract",the entire class is abstract.

  • 8/12/2019 Sudhir c Sharp Book

    76/76

    www.dbakings.com

    Method signatures must be unique for the types which aredefined partially.