Create properties with backend fields - CSharp Custom Type

CSharp examples for Custom Type:Field

Description

Create properties with backend fields

Demo Code

using System;/*from  www .j  a va2  s .c  om*/
class Student {
   private string code = "N.A";
   private string name = "not known";
   private int age = 0;
   public string Code {
      get {
         return code;
      }
      set {
         code = value;
      }
   }
   public string Name {
      get {
         return name;
      }
      set {
         name = value;
      }
   }
   public int Age {
      get {
         return age;
      }
      set {
         age = value;
      }
   }
   public override string ToString() {
      return "Code = " + Code +", Name = " + Name + ", Age = " + Age;
   }
}
class MainClass {
   public static void Main() {
      Student s = new Student();
      s.Code = "001";
      s.Name = "PinkMan";
      s.Age = 9;
      Console.WriteLine("Student Info: {0}", s);
      s.Age += 1;
      Console.WriteLine("Student Info: {0}", s);
   }
}

Result


Related Tutorials