Introduction:
Properties (The definition):
Roadmap:

- Diving in OOP (Day 1): Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism)
- Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
- Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
- Diving in OOP (Day 4): Polymorphism and Inheritance (All about Abstarct classes in C#)
- Diving in OOP (Day 5): All about access modifiers in C# (Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields)
- Diving in OOP (Day 6): Understanding Enum in C# (A Practical Approach)
- Diving into OOP (Day 7): Properties in C# (A Practical Approach)
- Diving into OOP (Day 8): Indexers in C# (A Practical Approach)
- Diving into OOP (Day 9): Understanding Events in C# (An Insight)
Properties (The explanation):
Lab1
Properties.cs
namespace Properties { public class Properties { public string Name{} } }
Output
Lab2
Properties.cs
Properties.cs:
namespace Properties
{
public class Properties
{
public string Name
{
get
{
return “I am a Name property”;
}
}
}
}
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
Console.WriteLine(properties.Name);
Console.ReadLine();
}
}
}
Output

Get accessor
Properties.cs
using System;
namespace Properties
{
public class Properties
{
public string Name
{
get
{
return “I am a Name property”;
}
}
public int Age
{
get
{
DateTime dateOfBirth=new DateTime(1984,01,20);
DateTime currentDate = DateTime.Now;
int age = currentDate.Year – dateOfBirth.Year;
return age;
}
}
}
}
Age
” property in the same way as done for Name.Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
Console.WriteLine(properties.Name);
Console.WriteLine(“My age is “ + properties.Age);
Console.ReadLine();
}
}
}
Output

Point to remember
Set accessor
Lab1
Properties.cs
using System; namespace Properties { public class Properties { public string Name { get { return "I am a Name property"; } }
public int Age
{
get
{
DateTime dateOfBirth = new DateTime(1984, 01, 20);
DateTime currentDate = DateTime.Now;
int age = currentDate.Year – dateOfBirth.Year;
Console.WriteLine(“Get Age called”);
return age;
}
set
{
Console.WriteLine(“Set Age called “ + value);
}
}
}
}
Age
” property in the same way as done for Name.Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(“My age is “ + properties.Age);
Console.ReadLine();
}
}
}
Output

Set
” in Age property too. Everything else remains same. Now when I call Name property, it works as was working earlier.Since we used “Set” so now we are allowed to set the value of a property. When I do properties.Age = 40;
that means I am setting value 40 to that property. We can say a property can also be used to assign some value.In this case Set accessor is called, as soon as we set a value to property. Later on when we access that same property, again our get accessor gets called which returns value with some custom logic. We have a drawback here, as we see, whenever we call get we get the same vale and not the value that we assigned to that property i.e. because get has its custom fixed logic. Let’s try to overcome this situation.Lab2
Properties.cs

using System; namespace Properties { public class Properties { private string name; private int age;
public string Name
{
get { return name; }
set
{
Console.WriteLine(“Set Name called “);
name = value;
}
}
public int Age
{
get { return age; }
set
{
Console.WriteLine(“Set Age called “);
age = value;
}
}
}
}
Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = “Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(“My age is “ + properties.Age);
Console.ReadLine();
}
}
}
Output

Point to remember
Lab3
Properties.cs
using System; namespace Properties { public class Properties { private string name; private int age;
public string Name { get; set; }
public int Age { get; set; }
}
}
Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = “Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(“My age is “ + properties.Age);
Console.ReadLine();
}
}
}
Output

public string Name { get; set; } public int Age { get; set; }
Readonly
Properties.cs
using System; namespace Properties { public class Properties { private string name="Akhil"; private int age=32;
public string Name
{
get { return name; }
}
public int Age { get { return age; } }
}
}
Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = “Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(“My age is “ + properties.Age);
Console.ReadLine();
}
}
}
Properties.Properties.Age
‘ cannot be assigned to — it is read onlyProperties.Properties.Name
‘ cannot be assigned to — it is read onlyAge
and Name
property by,properties.Name = "Akhil"; properties.Age = 40;
Write-Only
Properties.cs
using System; namespace Properties { public class Properties { private string name; private int age;
public string Name
{
set { name=value; }
}
public int Age { set { age = value; } }
}
}
Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties properties=new Properties();
properties.Name = “Akhil”;
Console.WriteLine(properties.Name);
properties.Age = 40;
Console.WriteLine(“My age is “ + properties.Age);
Console.ReadLine();
}
}
}
Console.WriteLine(properties.Name); Console.WriteLine("My age is " + properties.Age);
Insight of Properties in C#
Lab1
Properties.cs
using System; namespace Properties { public class Properties { private string name;
public string Name
{
set { name=value; }
}
public string Name
{
get { return name; }
}
}
}
Lab2
Properties.cs
using System; namespace Properties { public class Properties { private string name;
public string name
{
set { name=value; }
get { return name; }
}
}
}
Properties vs Variables
Point of difference | Variable | Property |
Declaration | Single declaration statement | Series of statements in a code block |
Implementation | Single storage location | Executable code (property procedures) |
Storage | Directly associated with variable’s value | Typically has internal storage not available outside the property’s containing class or moduleProperty’s value might or might not exist as a stored element 1 |
Executable code | None | Must have at least one procedure |
Read and write access | Read/write or read-only | Read/write, read-only, or write-only |
Custom actions (in addition to accepting or returning value) | Not possible | Can be performed as part of setting or retrieving property value |
Static Properties
Properties.cs
using System; namespace Properties { public class Properties { public static int Age { set { Console.WriteLine("In set static property; value is " + value); } get { Console.WriteLine("In get static property"); return 10; } } } }
Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties.Age = 40;
Console.WriteLine(Properties.Age);
Console.ReadLine();
}
}
}
Output

Age
property. When I tried to access it, you can see it is accessed via class name, like all static members are subjected to. So properties also inherit the static functionality like all c# members, no matter it is variable or a method. They’ll be accessed via class name only.Properties return type
Lab1
Properties.cs
using System;
namespace Properties
{
public class Properties
{
public void AbsProperty
{
get
{
Console.WriteLine(“Get called”);
}
}
}
}
Point to remember
Lab2
Properties.cs
using System;
namespace Properties
{
public class Properties
{
public int Age
{
set { return 5; }
}
}
}
using System;
namespace Properties
{
public class Properties
{
public int Age
{
set { return ; }
}
}
}
Value Keyword
using System;
namespace Properties
{
public class Properties
{
public string Name
{
set { string value; }
}
}
}
Abstract Properties
Lab1
Properties.cs
using System;
namespace Properties
{
public abstract class BaseClass
{
public abstract int AbsProperty { get; set; }
}
public class Properties : BaseClass
{
public override int AbsProperty
{
get
{
Console.WriteLine(“Get called”);
return 10;
}
set { Console.WriteLine(“set called,value is “ + value); }
}
}
}
Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties prop=new Properties();
prop.AbsProperty = 40;
Console.WriteLine(prop.AbsProperty);
Console.ReadLine();
}
}
}
Output

Absproperty
. Since the property is abstract it follows the rules of being abstract as well. I inherited my “Properties
” class from BaseClass
and given the body to that abstract property. Since the property was abstract I have to override it in my derived class to add functionality to it. So I used override keyword in my derived class.Properties
” class.Point to remember
Lab2
Properties.cs
using System;
namespace Properties
{
public abstract class BaseClass
{
public abstract int AbsProperty { get; }
}
public class Properties : BaseClass
{
public override int AbsProperty
{
get
{
Console.WriteLine(“Get called”);
return 10;
}
set { Console.WriteLine(“set called,value is “ + value); }
}
}
}
Program.cs
using System;
namespace Properties
{
class Program
{
static void Main(string[] args)
{
Properties prop=new Properties();
prop.AbsProperty = 40;
Console.WriteLine(prop.AbsProperty);
Console.ReadLine();
}
}
}
Output
AbsProperty
in Base class. All the code remains same. Now here we are trying to override the set accessor too in derived class, that is missing in base class, therefore compiler will not allow you to override a successor that is not declared in base class, hence resulted into a compile time error.Point to remember
Properties in Inheritance
Properties.cs
using System;
namespace Properties
{
public class PropertiesBaseClass
{
public int Age
{
set {}
}
}
public class PropertiesDerivedClass:PropertiesBaseClass
{
public int Age
{
get { return 32; }
}
}
}
Program.cs
namespace Properties { class Program { static void Main(string[] args) { PropertiesBaseClass pBaseClass=new PropertiesBaseClass(); pBaseClass.Age = 10; PropertiesDerivedClass pDerivedClass=new PropertiesDerivedClass(); ((PropertiesBaseClass) pDerivedClass).Age = 15; pDerivedClass.Age = 10; } } }
PropertiesBaseClass
and second in Derived i.e. PropertiesDerivedClass
. I purposely declared set accessor in Base class and get in Derived class for the same property name i.e. Age. Now this case may give you the feeling that when compiled, our code of property Age
will become one, i.e. it will take set from Base class and get from derived class and combine it into a single entity of Age property.But this is practically not the case. The compiler treats both these properties differently, and does not consider them to be same. In this case the property in derived class actually hides the property in base class , they are not the same but independent properties.The same concept of method hiding applies here too. You can read about hiding inhttp://www.codeproject.com/Articles/774578/Diving-in-OOP-Day-Polymorphism-and-Inheritance-Dyn.((PropertiesBaseClass) pDerivedClass).Age = 15;
pDerivedClass.Age = 10;
because derived class property has no “set” accessor.Summary

- The variable used for property should be of same data type as the data type of the property.
- A property cannot have a void return type.
- If one do not mark property defined in derived class as override, it will by default be considered as new.
- You cannot override an accessor that is not defined in a base class abstract property.
- Get accessor is only used to read a property value. A property having only get cannot be set with any value from the caller.
Conclusion

Read more:
- C# and ASP.NET Questions (All in one)
- MVC Interview Questions
- C# and ASP.NET Interview Questions and Answers
- Web Services and Windows Services Interview Questions
Other Series
My other series of articles:
For more informative articles visit my Blog.
7 comments