Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Class and Object in C#

Class: Class Contains Data members for storing data and member function to perform action on that data.

Object: Its class variable. Used to access class methods.

Example: 

using System;

namespace ClassObject {

  class Dog {
    string breed;

    public void bark() {
      Console.WriteLine("Bark Bark !!");
      
    }

    static void Main(string[] args) {

      // create Dog object 
      Dog bullDog = new Dog();

      // access breed of the Dog 
      bullDog.breed = "Bull Dog";
      Console.WriteLine(bullDog.breed);

      // access method of the Dog
      bullDog.bark();   

      Console.ReadLine();
     
    }
  }
}

Happy Coding!

Thanks.

Post a Comment

0 Comments