Class with Constructors in C#

Todays session we are gonna learn how to create a class with more then one constructors and what are the components of a class . Constructors are used to initialize the data members present in a class.

using System;
namespace Test
{
  class Animal
  {
private string _animalType; // data members or fields
private int _animalCount;    // data members or fields


public Animal()           // Default constructor
{
_animalType="";
_animalCount=0;
}

public Animal(string animalType,int animalCount)  // parameterized constructor
{
_animalType=animalType;
_animalCount=animalCount;
    }

public string animalType              // Property
{
get{ return _animalType;}        // Accessor
set{ _animalType=value;}         // Mutator
}

public int animalCount              //Property
{
get{return _animalCount;}      // Accessor
set{_animalCount=value;}       // Mutator
}

  public void AnimalDetails()      // Data Behaviour  or method
  {
  Console.WriteLine("Animal Type :"+_animalType);
  Console.WriteLine("Animal Count :"+_animalCount);
  }
}
public class First
{
public static void Main()
{
Animal animal = new Animal();   // Instance or Object creation
//Animal animal = new Animal("Dog",5); // Instance or Object creation
animal.AnimalDetails();      // Accessing public methods
}


}

}

Output
1.
Animal Type :
Animal Count :0

2.
Animal Type :Dog
Animal Count :5

Comments

Popular posts from this blog

Types of Architects

Basic measurements

Search html table contents based on its td using Jquery