By 03/02/2016 1 Comments

Difference between a Field and a Property in C#?

  • Fields are also called instance variables.
  • The declaration of fields should be private.
  • Fields itself contains different type of data.
  • Properties are main source which accesses the private fields.
  • Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.
  • Fields itself can be accessed via get and set type properties.

Example:

using System;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {

            Test test = new Test();
            test.myField = "tutorialstown.com";
            Console.WriteLine("Website is: " + test.myField);
            Console.ReadKey();
        }
    }

    class Test
    {
        private string _site; // private field declaration.

        public string myField
        {
            get
            {
                return _site;
            }

            set
            {
                _site = value;
            }
        }    
    }
}






Posted in: C#.NET, Solutions

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 .