3 Replies - 25111 Views - Last Post: 09 December 2008 - 06:21 PM Rate Topic: -----

#1 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Virtual property in C# ? Is there such animal?

Post icon  Posted 09 December 2008 - 02:41 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Virtual property in C# ? Is there such animal?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Virtual property in C# ? Is there such animal?

Posted 09 December 2008 - 06:00 PM

View PostZammy, 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?


??? 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


Was This Post Helpful? 2
  • +
  • -

#3 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

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.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,804
  • Joined: 16-October 07

Re: Virtual property in C# ? Is there such animal?

Posted 09 December 2008 - 06:21 PM

View PostZammy, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1