---
title: "Is there a way to only add code to a method without overriding it completely in c#?"  
description: "Is there a way to only add code to a method without overriding it completely in c#?"  
author: "Kate Smith"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1568/is-there-a-way-to-only-add-code-to-a-method-without-overriding-it-completely-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Is there a way to only add code to a method without overriding it completely in c#?

Is there any way to only [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) to a [method](https://www.mindstick.com/forum/166/webservice-method) from a subclass without overriding it completely like the "override" + "[virtual](https://yourviews.mindstick.com/view/81341/the-oh-so-fight-for-democracy-virtual-rally-scenes)" does in c#. I find myself [writing](https://www.mindstick.com/articles/12997/5-essential-tips-on-resume-writing-for-business-analysts) some [duplicate](https://www.mindstick.com/forum/160911/sql-query-to-find-duplicate-records-in-a-table-in-sql-server) code in my override methods. And not sure what to do about it

## Replies

### Reply by Samuel Fernandes

Hello Kate!

You can use override and virtual with a different mechanism. For example,

```
class MyBase
    {
        private int MyVar;
        public virtual void DoStuff(int i , int j)
        {
            MyVar = i + j; //This is your common code which is added in base class

        }
    }
     class OverridClass : MyBase
    {
        private int MyNewCount;
        public override void DoStuff(int i, int j)
        {
            MyNewCount = i + j;
            base.DoStuff(i, j); //This is how you reuse your common code and write the code which is more specific to this method
         }
    }
```


---

Original Source: https://www.mindstick.com/forum/1568/is-there-a-way-to-only-add-code-to-a-method-without-overriding-it-completely-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
