Class and Method
           Programming in C#




              tnngo2@gmail.com
Class and Objects
Object-oriented programming

      Programming languages designed based on data and ways to
      manipulate data.

      Procedural Languages (Pascal, C) focus on ways


      Object-oriented Languages (C#, Java) focus on data
Features

      Abstraction
      Encapsulation
      Inheritance
      Polymorphism


                      Focus only the required information from objects
Features

      Abstraction

      Encapsulation
      Inheritance
      Polymorphism



                      Hide details of what a class contains
Features

      Abstraction

      Encapsulation

      Inheritance
      Polymorphism


                      Create a new class based on the attributes
                      and methods of an existing class
Features

      Abstraction

      Encapsulation
      Inheritance

      Polymorphism

                      Behave differently in different situations
Class
Class – should & cannot

      Should
               be a noun
               first letter capitalized
               simple, descriptive, meaningful
      Cannot
               in mixed case
               C# keyword
               begin with a digit except @, _
Methods
Method – can & cannot

     Can
           begin with a letter, _ or @
     Cannot
           C# keyword
           contain space
           begin with digit
Invoking methods

      https://gist.github.com/2349623
Static methods

      called without creating any objects of the class
      refer only to static variables and other static methods of class.

      https://gist.github.com/2349658
Static methods

      Only one copy of static variable is shared by all the objects of the
      class.
Question?

     https://gist.github.com/2349708
Access Modifiers
Access Modifiers
Ref

      causes arguments to be passed in a method by reference


      https://gist.github.com/2344456
Out

      similar to ref but no
                          required the variables that are passed by
      reference to be initialized

      https://gist.github.com/2344483
Method overloading
Method overloading

      every method has a signature which comprises
              the number of params
              the data types of params
              the order of params
Should and avoid

      Methods to be overloaded should perform the same task

      The signatures of the overloaded must be unique

      Return type is not a part of the signature

      The ref and out parameters can be included as a part of the
      signature
This keyword

      refer to the current object of the class.

      cannot use this keyword with static variables and method

      https://gist.github.com/2344534
Question?

     https://gist.github.com/2349708
Constructors and Destructors
Constructors

      initialization

      no return type

      possible to have overloaded constructors

      https://gist.github.com/2344570
Default Constructors

      C# creates a default constructors for a class if no constructor is
      specified within the class. It automatically initializes all the numeric
      data type instance variables to zero.

      If you define a constructor in the class, the default constructor is no
      longer used.
Static Constructors

      initialize static variables of the class and to perform a particular
      action only once.

      is invoked before any static member of the class is accessed.

      does not take any parameters and does not use any access modifiers
      because it is invoked directly by the CLR instead of the object.

      Certainly, cannot access non-static data member.

      https://gist.github.com/2344643
Constructor Overloading


      https://gist.github.com/2344673
Destructors

      invoked automatically when the objects are not in use

      only one destructor in a class

      cannot be overloaded or inherited

      cannot be explicitly invoked


      cannot specify access modifiers and cannot take parameters

      https://gist.github.com/2344708
Garbage Collector

6 class and methods

  • 1.
    Class and Method Programming in C# tnngo2@gmail.com
  • 2.
  • 3.
    Object-oriented programming Programming languages designed based on data and ways to manipulate data. Procedural Languages (Pascal, C) focus on ways Object-oriented Languages (C#, Java) focus on data
  • 4.
    Features Abstraction Encapsulation Inheritance Polymorphism Focus only the required information from objects
  • 5.
    Features Abstraction Encapsulation Inheritance Polymorphism Hide details of what a class contains
  • 6.
    Features Abstraction Encapsulation Inheritance Polymorphism Create a new class based on the attributes and methods of an existing class
  • 7.
    Features Abstraction Encapsulation Inheritance Polymorphism Behave differently in different situations
  • 8.
  • 9.
    Class – should& cannot Should be a noun first letter capitalized simple, descriptive, meaningful Cannot in mixed case C# keyword begin with a digit except @, _
  • 10.
  • 11.
    Method – can& cannot Can begin with a letter, _ or @ Cannot C# keyword contain space begin with digit
  • 12.
    Invoking methods https://gist.github.com/2349623
  • 13.
    Static methods called without creating any objects of the class refer only to static variables and other static methods of class. https://gist.github.com/2349658
  • 14.
    Static methods Only one copy of static variable is shared by all the objects of the class.
  • 15.
    Question? https://gist.github.com/2349708
  • 16.
  • 17.
  • 18.
    Ref causes arguments to be passed in a method by reference https://gist.github.com/2344456
  • 19.
    Out similar to ref but no required the variables that are passed by reference to be initialized https://gist.github.com/2344483
  • 20.
  • 21.
    Method overloading every method has a signature which comprises the number of params the data types of params the order of params
  • 22.
    Should and avoid Methods to be overloaded should perform the same task The signatures of the overloaded must be unique Return type is not a part of the signature The ref and out parameters can be included as a part of the signature
  • 23.
    This keyword refer to the current object of the class. cannot use this keyword with static variables and method https://gist.github.com/2344534
  • 24.
    Question? https://gist.github.com/2349708
  • 25.
  • 26.
    Constructors initialization no return type possible to have overloaded constructors https://gist.github.com/2344570
  • 27.
    Default Constructors C# creates a default constructors for a class if no constructor is specified within the class. It automatically initializes all the numeric data type instance variables to zero. If you define a constructor in the class, the default constructor is no longer used.
  • 28.
    Static Constructors initialize static variables of the class and to perform a particular action only once. is invoked before any static member of the class is accessed. does not take any parameters and does not use any access modifiers because it is invoked directly by the CLR instead of the object. Certainly, cannot access non-static data member. https://gist.github.com/2344643
  • 29.
    Constructor Overloading https://gist.github.com/2344673
  • 30.
    Destructors invoked automatically when the objects are not in use only one destructor in a class cannot be overloaded or inherited cannot be explicitly invoked cannot specify access modifiers and cannot take parameters https://gist.github.com/2344708
  • 31.