Answer Posted / uday
Properties are not like variables, rather they are calling
methods. They dont be allocated memory like variables.
They(get,set accessors) will be called automatically when
we assigne value or refer the value.
class ProperyCls
{
string name;
public string Name
{
get
{
return name;
}
set
{
Console.WriteLine("In Set method the value
is "+value);
name = value;
}
}
}
class Program
{
static void Main(string[] args)
{
ProperyCls pObj = new ProperyCls();
//Here the set method will be automatically
called
pObj.Name = "Hello";
//Here the get method will be called
automatically
Console.WriteLine("In get method the value is "
+ pObj.Name);
Console.ReadKey();
}
}
Indexers are different in the context when i create
multiple instances of the class and assign properties for
each of the instances. Indexers can be defined with "this"
keyword.
class ProperyCls
{
public int this[int i]
{
get
{
return 20;
}
set
{
Console.WriteLine("In set the value is " +
value + "at index" + i);
}
}
}
class Program
{
static void Main(string[] args)
{
ProperyCls pObj = new ProperyCls();
pObj[1] = 10;
pObj[2] = 30;
Console.WriteLine("In get method the value
returned is " + pObj[1]);
Console.ReadKey();
}
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What are custom exceptions in C#?
Why do I get a syntax error when trying to declare a variable called checked?
What is the difference between a class and an object c#?
What is wpf c#?
What is a verbatim string literal and why do we use it?
Explain the 3 types of properties in c# with an example?
What are events in C#?
What is signature c#?
Is string a class in c#?
Describe the overview of clr integration.
Can we make a class private in c#?
Can an abstract class have a constructor c#?
What is the difference between read and readline in c#?
What are the types of class in c#?
Explain the difference between a Private Assembly and a Shared Assembly