I was re-factoring some code and it turned out that it would work great if I could make a certain property virtual.
Of course this did not work in c#2.0. I was wondering if C#3.0 has it and if 4.0 will?
My logic is that property is anyway a method, why not be able to make it virtual and override it ?
Of course I will do a workaround and make a SetBlaBla() method but it is just not neat. I love properties.
Evgeni
Virtual property in C# ? Is there such animal?I was refactoring some code...
Page 1 of 1
3 Replies - 22663 Views - Last Post: 09 December 2008 - 06:21 PM
Replies To: Virtual property in C# ? Is there such animal?
#2
Re: Virtual property in C# ? Is there such animal?
Posted 09 December 2008 - 06:00 PM
Zammy, on 9 Dec, 2008 - 03:41 PM, said:
I was re-factoring some code and it turned out that it would work great if I could make a certain property virtual.
Of course this did not work in c#2.0. I was wondering if C#3.0 has it and if 4.0 will?
Of course this did not work in c#2.0. I was wondering if C#3.0 has it and if 4.0 will?
??? This has always worked as far as I know:
using System;
namespace ConsoleApplication1 {
class Critter {
public virtual string Eats { get { return "Air"; } }
public override string ToString() {
return this.GetType().Name + " eats " + Eats;
}
}
class Dog : Critter {
public override string Eats { get { return "Cat"; } }
}
class Lion : Critter {
public override string Eats { get { return "Dog"; } }
}
class Program {
static void Main(string[] args) {
Console.WriteLine(System.Environment.Version);
Console.WriteLine(new Critter());
Console.WriteLine(new Dog());
Console.WriteLine(new Lion());
Console.ReadLine();
}
}
}
Results:
2.0.50727.42 Critter eats Air Dog eats Cat Lion eats Dog
#3
Re: Virtual property in C# ? Is there such animal?
Posted 09 December 2008 - 06:03 PM
Hmm....
I thought it did not work. My mistake then I am really sorry.
I thought it did not work. My mistake then I am really sorry.
#4
Re: Virtual property in C# ? Is there such animal?
Posted 09 December 2008 - 06:21 PM
Zammy, on 9 Dec, 2008 - 07:03 PM, said:
I am really sorry.
No worries. Don't be sorry. I didn't know for sure until I ran the program.
Never be afraid to just experiment; it's usually the best way to tell how something works. Inheritance is a good example of this; you can read libraries worth of theory on it, but it's not really going to make sense until you actually use it.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|