Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Can a base class property be invoked from derived class

Tags:

I have a base class with a property which has a setter method. Is there a way to invoke the setter in the base class from a derived class and add some more functionality to it just like we do with overriden methods using the base keyword.

Sorry I should have added an example. Here is an example. Hope I get it right:

public class A  {     public abstract void AProperty      {         set          {             // doing something here         }     } }  public class B : A  {        public override void AProperty      {         set          {             // how to invoke the base class setter here              // then add some more stuff here         }     }    } 
like image 795
Shahid Avatar asked Feb 24 '11 17:02

Shahid


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

EDIT: the revised example should demostrate the order of invocations. Compile as a console application.

class baseTest  {     private string _t = string.Empty;     public virtual string t {         get{return _t;}         set         {             Console.WriteLine("I'm in base");             _t=value;         }     } }  class derived : baseTest {     public override string t {         get { return base.t; }         set          {             Console.WriteLine("I'm in derived");             base.t = value;  // this assignment is invoking the base setter         }     } }  class Program {      public static void Main(string[] args)     {         var tst2 = new derived();         tst2.t ="d";          // OUTPUT:         // I'm in derived         // I'm in base     } } 
like image 79
Paolo Falabella Avatar answered Sep 21 '22 02:09

Paolo Falabella