Create auto property without using underline field - CSharp Custom Type

CSharp examples for Custom Type:Property

Description

Create auto property without using underline field

Demo Code

using System;/*from   w  w  w  . j  a v a2 s.  c om*/
using System.Collections.Generic;
using System.Text;
public class Person
{
   public static int MinAge = 18;
   public static int MaxAge = 70;
   private readonly string _firstName;
   private readonly string _lastName;
   public string FirstName
   {
      get
      {
         return _firstName;
      }
   }
   public string LastName
   {
      get
      {
         return _lastName;
      }
   }
   public int Age { get; set; }
   private decimal _salary;
   public Person(string firstName, string lastName, int age, decimal salary = 45000)
   {
      _firstName = firstName;
      _lastName = lastName;
      Age = age;
      //default value
      _salary = salary;
   }
}
class Program
{
   static void Main(string[] args)
   {
      Person person = new Person("J", "S", 44);
      person.Age = 17;
      Console.WriteLine("{0} {1} - age: {2}", person.FirstName, person.LastName, person.Age);
      if (person.Age < Person.MinAge)
      {
         Console.WriteLine("Too young");
      }
   }
}

Result


Related Tutorials