1

I was reading about extension methods and how they can extend classes with new methods without having to change the class code definition. I wanted to know if there was any similar way by which I can add a new data member (like a List or an array) to an existing class and use the data member to store information related to the class?

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
AvinashK
  • 3,309
  • 8
  • 43
  • 94

6 Answers6

6

Yes you can extend that class using inheritence.

public class MyClass 
{
    ...
}

public ExtendedClass: MyClass
{
   public int ExtraField {get; set;}
}

This way you have all of the members and methods (except private) that exist on the base.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
3

With extension methods you can only extend the functionality of a class.

What you are looking for can be solved with:

  • Aggregation OR
  • Inheritance

This post may help you on deciding which one to use in your case: Inheritance vs. Aggregation

Community
  • 1
  • 1
KrisztiƔn Balla
  • 19,223
  • 13
  • 68
  • 84
2

You can use inheritance or composition for that.

Inheritance Example:

public class Student
{
   int age;// all props
}

public class MAStudent : Student // MSStudent is a student with extra stuff.
{
   float maAverage;
}

Composition Example:

public class Student
{
   int age;// all props
}

public class MAStudent
{
   Student student;
   float maAverage;

   // use student's functions inside the class
}

Inheritance is the easiest way to do things.
The problem with it is that it makes your classes coupled.
The good perk with inheritance that you can access every protected+ property \ method.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
2

There is no way of directly adding members to a specific class. If the class isn't sealed, you may extend that class by using inheritance. If it is sealed, you may compose yourself a new class which encapsulates the specific class you wanted to extend and extend the implementation.

For example, if you have MyClass which isn't sealed and you want to extend it, simply inherit:

public class MyExtendedClass : MyClass
{
  // Add extra logic
}

or, as for composing a new class yourself, you may do the following:

public class MyExtendedClass
{
   private MyClass _class;
   public string MyExtraString { get; set; }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
2

Although the other answers seem to be correct, the answer to your question IMHO, is that it is not possible to extend an existing class with new properties in the way that extension methods do that. Once a class is defined, you cannot 'add' things to it.

Extension method is an exception, since that is just syntactic sugar for a static helper class.

Maarten
  • 22,527
  • 3
  • 47
  • 68
0

Also you can write something based on extension methods like this

public class ExistingClass 
{

}

public static class ExtendingExistingClass 
{
    private static Dictionary<ExistingClass,List> _values = new Dictionary<ExistingClass,List>();

    public List GetMyNewField(this ExistingClass t)
    {
       List res = null;
       _values.TryGetValue(t, out res);
       return res; 
    }

    public void SetMyNewField(this ExistingClass t, List value)
    {
       _values[t] = value;
    }
}
IL_Agent
  • 707
  • 3
  • 16