Create A constructor to default values in the data members. - CSharp Custom Type

CSharp examples for Custom Type:Constructor

Description

Create A constructor to default values in the data members.

Demo Code

class die// ww w  .java 2 s.co  m
{
   public int sides;
   public int value;
   static System.Random rnd = new System.Random();
   public int roll()
   {
      value = (int) ((rnd.NextDouble() * sides) + 1);
      return value;
   }
   public die()
   {
      sides = 6;
      value = 0;
   }
}
class diceclass
{
   public static void Main()
   {
      die dice1 = new die();
      die dice2 = new die();
      dice1.sides = 6;
      dice2.sides = 12;
      dice1.roll();
      dice2.roll();
      System.Console.WriteLine("dice 1; sides = {0}, value = {1}", dice1.sides, dice1.value);
      System.Console.WriteLine("dice 2; sides = {0}, value = {1}", dice2.sides, dice2.value);
   }
}

Result


Related Tutorials