c#(second day)

Upload: mridul-chatterjee

Post on 08-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 C#(Second Day)

    1/30

    C#

    HCL Infosystems Ltd.

  • 8/6/2019 C#(Second Day)

    2/30

    Hierarchy

    Namespace

    Classes, Structure, Enumerators

    Field, Methods, Indexeres, Delegates

    Examples

    System namesapace

    Console class

    Write() method

  • 8/6/2019 C#(Second Day)

    3/30

    Importing namespaces

    Use the using keyword

    using System;

    Console.WriteLine(Hello);

  • 8/6/2019 C#(Second Day)

    4/30

    Creating Aliases

    A method to define the alias or shortcut to some class or interface or

    structure

    Use the using keyword

    using =;

    e.g.

    using c=System.Console;

    c.WriteLine(Hello);

  • 8/6/2019 C#(Second Day)

    5/30

    Data types

    Divided into two categories

    Primitive or Value type

    Reference Types

  • 8/6/2019 C#(Second Day)

    6/30

    Primitive or Value type

    That holds values

    Integrals

    byte byte

    short 2 byte

    int 4 byte

    long 8 byte

    sbyte

    ushort

    uint

    ulong

    Floatings

    floa

    t 4 bytes 6 dec

    plac

    e double 8 bytes 14 dec place

    decimal 16 bytes 28 dec place

    Character

    char 2 bytes

    Booleans

    bool 2 bytes

  • 8/6/2019 C#(Second Day)

    7/30

    Reference Types

    string

    object

    Difference between in value type and reference type

    Value types are stored in stack and reference in heap

    Value types can never have null value assigned

  • 8/6/2019 C#(Second Day)

    8/30

    View contents of a exe file or MSIL code

    Use ILDASM tool to view MSIL code

    ILDASM filename.exe

  • 8/6/2019 C#(Second Day)

    9/30

    Reading Data from Keyboard

    Use ReadLine() method of Console class

    To convert data from String to other value types use

    Parse() method of data types

    We can also methods from Convert class

    long Convert.ToInt64()

    int Convert.ToInt32()

    short Convert.ToInt16()

  • 8/6/2019 C#(Second Day)

    10/30

    Usage of @

    Neutralize the meaning of escape sequence characters

    Use keyword as identifier

    Remove the escape sequence effect

    Break stings in multi-line

    string s=@ c:\amit;

    Spead a string into multiple lines

    string s=@ Amit

    Kumar

    Sharm

    a;

    Allow to use a keyword as indentfier

    int @float;

  • 8/6/2019 C#(Second Day)

    11/30

    Conditional Statements

    if

    switch

    ternary operator (?:)

    Shortcut of if-else

    Condition?true part:false part;

  • 8/6/2019 C#(Second Day)

    12/30

    Loops

    while loop

    do-while loop

    for loop

    foreach loop works with arrays and collections

  • 8/6/2019 C#(Second Day)

    13/30

    Class and Object

    Aclass is a template or set of specifications to create similar set of

    objects

    An object is the real entity that takes some memory space for its

    fields and executes its members

  • 8/6/2019 C#(Second Day)

    14/30

    Types ofmembers

    Non-static or instance members always require an object to call

    them

    Static members or Class members Do not require any object to

    call them. Accessible directly though the class name. static keyword

    is required.

  • 8/6/2019 C#(Second Day)

    15/30

    Classification ofmembers

    Fields to hold the value

    Variable - default

    Constants const keyword required and value must be

    assigned while declaration

    const double pi=31.4;

    ReadOnly Fields Value can be assigned directly or using

    constru

    ctor. readonly keyword required

  • 8/6/2019 C#(Second Day)

    16/30

    Methods to do some action

    Constructor

    Destructor

    Concrete Methods or General methods

    Abstract methods

    Sealed method

  • 8/6/2019 C#(Second Day)

    17/30

    Constructor

    Special method which has the same name as

    class

    No return type

    Used to initialize fields ofaclass

    Can be overlo

    aded

    Can be private

  • 8/6/2019 C#(Second Day)

    18/30

    Destructor

    Special methods which has the same name

    as class name preceded by tild (~)

    Calls automatically when object releases the

    memory

    Cannot be overloaded

    By default is public, public keyword is not

    allowed to use

  • 8/6/2019 C#(Second Day)

    19/30

    Abstract methods

    A methods having only the signature but no body

    contents

    Contents are provide by overriding the methods in

    child class

    If declared in class, abstract keyword is required and

    if declared inside an interface, no abstract keyword

    required

    Ifaclass contains any abstract method, it must be

    declared as abstract

  • 8/6/2019 C#(Second Day)

    20/30

    Sealed method

    A method that can never be overridden

    sealed keyword is required

  • 8/6/2019 C#(Second Day)

    21/30

    Difference between const and readonly field

    In const value must be give while declaring the

    constants

    const int n=5;

    Readonly field can have the value while

    declaration or using constructors

    readonly int n;

    readonly int n=5;

  • 8/6/2019 C#(Second Day)

    22/30

    Properties

    Special methods that looks like fields for outside world

    set and get are used to create the properties

  • 8/6/2019 C#(Second Day)

    23/30

    datatype variable;

    {

    set

    {

    Variable=value;

    }

    get

    {

    Return variable;

    }

    }

    Objectname.propetyname=anyvalue; //set

    Variable=objectname.propertyname; //get

  • 8/6/2019 C#(Second Day)

    24/30

    Types ofproperties

    Read/Write properties

    Having set and get

    Read Only properties

    Having only get

    Write Only properties

    Having only set

  • 8/6/2019 C#(Second Day)

    25/30

    Passing arguments to the methods

    Pass by value (default)

    Pass by reference (ref keyword)

    When values from actual arguments get passed to formal

    arguments and if we make any changes to format arguments,

    they are reflected in actual arguments

    Pass By output (out keyword)

    To senda

    variable to re

    ceive the output

  • 8/6/2019 C#(Second Day)

    26/30

    Difference between ref and out

    In pass by ref, variable must be initialized by in out no initialization

    required

  • 8/6/2019 C#(Second Day)

    27/30

    Pillars of OOPs

    Encapsulation

    All members should be bundled in one body

  • 8/6/2019 C#(Second Day)

    28/30

    Abstraction orData Hiding

    Provides accessibility control on the members

    It is based on certain keywords

    private

    public

    protected

    internal (default)

    protected internal

  • 8/6/2019 C#(Second Day)

    29/30

    Polymorphism

    A method to achieve multiple activities from an item

    It is achieve using the concept of overloading

    Method Overloading

    Operator Overloading

  • 8/6/2019 C#(Second Day)

    30/30

    Inheritance

    Provides re-usability ofcode

    .NET allows only single inheritance

    It allows to use multiple interfaces

    Use : operator to inherit aclass or interface into other

    class or interface