Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 38770 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > C# Interview Questions > What do you mean by properties in C#? .. ...

What do you mean by properties in C#?

Interview question and answer by: Abhisek | Posted on: 1/21/2010 | Category: C# Interview questions | Views: 3097 |
Ads
Ads


Answer:

Property acts as a cross link between the field and the method . Actually it behaves as a field. We can retrieve and store data from the field using property.

The compiler automatically translates the field like property into a call like special method called as 'accessor" . In property there are two accessor and that are used to save value and retrieve value from the field. The two properties are 'get' and 'set'.

The get property is used to retrieve a value from the field and the set property is used to assign a value to a field .

Depending on there use properties are categorised into three types,

ReadWrite Property :- When both get and set properties are present it is called as ReadWrite Property.

ReadOnly Property :- When there is only get accessor it is called as ReadOnly Property.

WriteOnly Property :- When there is only set accessor, it is called as WriteOnly Property.

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


 Responses

Posted by: Ch.purnima@gmail.com | Posted on: 04 Feb 2010 06:16:26 AM | Alert Moderator 

Set of functions that can be accessed directly from the client (in a similar way to the public fields of the class)

Properties Example:

//C#: Property
//Author: rajeshvs@msn.com
using System;
class MyClass
{
private int x;
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.X = 10;
int xVal = mc.X;
Console.WriteLine(xVal);//Displays 10
}
}

Remember that a property should have at least one accessor, either set or get. The set accessor has a free variable available in it called value, which gets created automatically by the compiler. We can't declare any variable with the name value inside the set accessor.

Note:
1. To create read-only property, simply omit the set accessor from the properties definition
2. To create write-only, omit the get accessor.
3. C# doesn’t permit setting different access modifiers to get and set. Only public get and protected set.

Ex: public string ForeName
{
get
{
return forename;
}
}
protected void setForeName(string value)
{
if(---) forename = “so so”
else forename = value;
}


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Abhisek

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 8:18:34 AM