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
Why singleton class is sealed?
Explain how many types of exception handlers are there in .net?
Is c# good for web development?
What is the signature of a method?
Define multicast delegate? How it be used?
What is sqlcommand in c#?
What are the Types of values mode can hold session state in web.config
What are the new features in c# 2.0?
Does the system.exception class have any cool features?
What is xml document how do you open it?
What is a interface in c#?
Suppose two interfaces have same method, so how will you implement these methods in derive class?
Can a struct inherit from an interface in c#?
How many types of constructors are available in c#?
What are floating point numbers?