By 22/07/2015 9 Comments

C# Auto-Implemented Properties

  • In C# a class can also have auto-implemented propertied.
  • Auto implemented properties concise the implementation of a regular properties without using get and set value logic explicitly.
  • When we implement an auto implemented property the compiler automatically creates an unnamed, unknown or anonymous backing private field (a storage location or backing storage) which holds the value (can only be accessed through get and set accessors).
  • An auto implemented property has get, set accessors without its body and terminated immediately by property.
  • An auto implemented property cannot be declared as a read only or write only property because it must contains both get and set accessors. We can apply access levels on get, set accessors to restrict these accessibility such as private, protected or public inside property.
  • By the use of an auto implemented property you have not full control on fields/instance variables because its access is limited.

General Syntax:

access-modifier type PropertyName { get; set; }

access-modifier It should be public.
type It defines a type of a property such as an int, a class, string, structures, enumeration etc.
PropertyName This is a name of a property, should be any meaningful name.
{ get; set; } That are get and set accessors without their body, this accessors are automatically handled by C# compiler implicitly, notice, each accessor can have access modifier such as public, private etc.


using System;
namespace csharpBasic
    class MarkSheet
        // Double type auto-implemented proeprties are declared.
        public double AspMarks { get; private set; }
        public double JavascriptMarks { get; private set; }
        public static double TotalMarks { get; set; }

        // Parametrized constructor with 2 parameters.
        public MarkSheet(double aspMarks, double javascriptMarks)
            this.AspMarks = aspMarks;
            this.JavascriptMarks = javascriptMarks;

        // Return type method returns obtain marks.
        public double GetObtainMarks()
            return this.AspMarks + this.JavascriptMarks;

        // Return type method returns percentage of obtain marks.
        public double GetPercentage(double obtainMarks, double totalMarks)
            return (obtainMarks / totalMarks) * 100.0;

    class Program
        static void Main(string[] args)
            // An Marksheet object creation.
            MarkSheet markSheet = new MarkSheet(80, 70);

            // Statement gets obtain marks by calling GetObtainMarks() method.
            double obtainMarks = markSheet.GetObtainMarks();

            // Statement assigns total marks (200) to a static auto implemented property TotalMarks.
            double totalMarks = MarkSheet.TotalMarks = 200;
            Console.WriteLine("Total Marks {0}", totalMarks);
            Console.WriteLine("Obtain marks: {0}", obtainMarks);
            Console.WriteLine("Pecentage: {0}", markSheet.GetPercentage(obtainMarks, totalMarks));
    /*  The Output will be:
        Total Marks 200
        Obtain marks: 150
        Pecentage: 75

Next Tutorial →

← Previous Tutorial

Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .