locked
What is properties in c#? RRS feed

  • Question

  • User1002530435 posted

    Hi,

          i am having a big question over the below line my project.Please help me some to understand its significance.Please note that i know what is properties.

    properties- to access private elements in our application by using a public access or its a encapsulation of sensitive information

    My queries are :

    Why we are given blank getset values?

    What is the use of the below line?

    public string Name { get; set; }

     

    Monday, January 5, 2015 1:01 AM

Answers

  • User197322208 posted

    Why we are given blank getset values?

    What is the use of the below line?

    public string Name { get; set; }

    Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

    http://msdn.microsoft.com/en-us/library/bb384054.aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2015 2:01 AM
  • User1002530435 posted

    Got some precise details about Properties

    In C# 2.0 version,we used to write the below code

    private CustomerID
    public int CustomerID
    {
    get { return _customerID; }
    set { _customerID = value; }
    }
    


    But in C#3.0,we used to write as below.

    public int CustomerID{get;set;}

    Because in C#3.0,the compiler creates private fields(beloe code in background) which are accessed through the property's get and set assessors. :)

    public int CustomerID
    {
    get { return _customerID; }
    set { _customerID = value; }
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2015 5:16 AM

All replies

  • User197322208 posted

    Why we are given blank getset values?

    What is the use of the below line?

    public string Name { get; set; }

    Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

    http://msdn.microsoft.com/en-us/library/bb384054.aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2015 2:01 AM
  • User724169276 posted

    Hello,

    Sometimes properties also holds some behavior or validation (for eg.) like if you are giving a property named AGE of datatype INT then its obvious you won't pass string values.So to make sure correct values are being passed we use get and set.

    What is the use of the below line?

    public string Name { get; set; }

     

    It means you are defining a property named Name.Here you are simply passing the values without any check.An example would be something like below:

    class TimePeriod
    {
        private double seconds;
    
        public double Hours
        {
            get { return seconds / 3600; }
            set { seconds = value * 3600; }
        }
    }

    Monday, January 5, 2015 2:16 AM
  • User1002530435 posted

    Got some precise details about Properties

    In C# 2.0 version,we used to write the below code

    private CustomerID
    public int CustomerID
    {
    get { return _customerID; }
    set { _customerID = value; }
    }
    


    But in C#3.0,we used to write as below.

    public int CustomerID{get;set;}

    Because in C#3.0,the compiler creates private fields(beloe code in background) which are accessed through the property's get and set assessors. :)

    public int CustomerID
    {
    get { return _customerID; }
    set { _customerID = value; }
    }

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 5, 2015 5:16 AM