C# Property | C# Properties - c# - c# tutorial - c# net




C# Property

C# Property

What is C# Property ?

  • C# Properites doesn't have storage location. C# Properites are extension of fields and accessed like fields.
  • The Properties have accessors that are used to set, get or compute their values.

Usage of C# Properties

  • C# Properties can be read-only or write-only.
  • We can have logic while setting values in the C# Properties.
  • We make fields of the class private, so that fields can't be accessed from outside the class directly. Now we are forced to use C# properties for setting or getting values.
 Data Queue

C# Properties Example

using System;
   public class Employee
    {
        private string name;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
   }
   class TestEmployee{
       public static void Main(string[] args)
        {
            Employee e1 = new Employee();
            e1.Name = "Sonoo Jaiswal";
            Console.WriteLine("Employee Name: " + e1.Name);
        }
    }

C# examples - Output :

Employee Name: Sonoo Jaiswal

C# Properties Example 2: having logic while setting value

using System;
   public class Employee
    {
        private string name; 

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value+" Wikitechy";
                
            }
        }
   }
   class TestEmployee{
       public static void Main(string[] args)
        {
            Employee e1 = new Employee();
            e1.Name = "Sonoo";
            Console.WriteLine("Employee Name: " + e1.Name);
        }
    }

C# examples - Output :

Employee Name: Sonoo Wikitechy

C# Properties Example 3: read-only property

using System;
   public class Employee
    {
        private static int counter;

        public Employee()
        {
            counter++;
        }
        public static int Counter
        {
            get
            {
                return counter;
            }
         }
   }
   class TestEmployee{
       public static void Main(string[] args)
        {
            Employee e1 = new Employee();
            Employee e2 = new Employee();
            Employee e3 = new Employee();
            //e1.Counter = 10;//Compile Time Error: Can't set value

            Console.WriteLine("No. of Employees: "+ Employee.Counter);
        }
    }

C# examples - Output :

No. of Employees: 3

Related Searches to C# Property | C# Properties

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×