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
Can we extend sealed class in c#?
What is datagrid c#?
What is delegate in c#?
In the page load event I assigned dropdownlist’s datasource property to a valid list. On the submit button click.. The same datasource property is coming as null. Why?
Can struct inherit from class c#?
Is datetime immutable c#?
What are the 2 broad classifications of data types available in c#?
What is virtual in c#?
Can you pass value types by reference to a method?
What do you mean by generic class in c#?
Can mvc be used for desktop applications?
Different between method overriding and method overloading?
In which way you can convert a value-type to a reference-type?
What is the use of base keyword? Tell me a practical example for base keyword’s usage?
What is different between Implicit conversion and Explicit conversion in C#?