9

Folks

I have a sealed class as follows. I want to extend this sealed class so as to add a method which will return average of x and y. This is not just extension method using "this" :( Could someone please help me understand the concept of "Extending Sealed class" and its "Real time usage and benefits"

class Sealed A       
{ 
    int a; int b;    
    int Add (int x, int y)    
    {
        return x+y;
    }
}

Thank you....

9
  • 3
    Stack Overflow is not a substitute for a book or tutorial. And, namely, the entire point of sealed is that you cannot extend it. Jun 27, 2012 at 20:30
  • @AnthonyPegram - Surviving without WSOIN?
    – Oded
    Jun 27, 2012 at 20:31
  • Sorry :( I was asked this in an interview :( I am new to this :(....Anthony, it makes sense now, it made me reason now :) Thank you....
    – Jasmine
    Jun 27, 2012 at 20:34
  • Ok guys, I may be wrong in putting the question. How to add extension method to sealed class ? I understand Sealed means we can derive only right ? :(
    – Jasmine
    Jun 27, 2012 at 20:36
  • 7
    I'm not sure what all the fuss and down votes are all about. I thought the intent of what Divine was trying to achieve was quite clear. Jun 27, 2012 at 20:53

2 Answers 2

23

As @ReedCopsey has already pointed out, the way to extend the functionality of a sealed class is with an Extension Method. Here is one that will do what you are asking:

public sealed class MyClass       
{ 
    int a; int b;    
    int Add (int x, int y)    
    {
        return x + y;
    }
}

public static class MyClassExtensions
{
    public static decimal Average(this MyClass value,  int x, int y)
    {
        return (x + y)/2M;
    }
}

Usage:

    var myClass = new MyClass();

    // returns 15
    var avg = myClass.Average(10, 20);

EDIT As requested, here is the all the code. Create a new Console Application in Visual Studio, replace all the code in the Program.cs file with the code below and run.

using System;

namespace ConsoleApplication1
{
    public sealed class MyClass
    {
        public int X { get; private set; }
        public int Y { get; private set; }

        public MyClass(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }

        int Add()
        {
            return this.X + this.Y;
        }
    }

    public static class MyClassExtensions
    {
        public static decimal Average(this MyClass value)
        {
            return (value.X + value.Y) / 2M;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var myClass = new MyClass(10, 20);
            var avg = myClass.Average();

            Console.WriteLine(avg);
            Console.ReadLine();
        }
    }
}
13
  • 2
    Perfect this is what I was expecting....EXACTLY....and oh yes now I can recognize when I wrote Average(this int x... interviewer stopped me and said this is sealed class and you can use this way..... So now I got to know from your post how to create extension method on sealed class.....so its different from creating extension method in normal class ? If I have to see the same class as normal class and not sealed, how will the implementation of the same be ? Thank you So much, your answer made me understand better... :)
    – Jasmine
    Jun 27, 2012 at 20:45
  • Nope, it's not different from creating an extension method for a normal (not sealed) class. It's exactly the same. Jun 27, 2012 at 20:47
  • 2
    Make sure you add a reference to the namespace that your MyClassExtensions class is in. E.g. if it is in the ConsoleApplication1.Extensions namespace, then add using ConsoleApplication1.Extensions; to the top of the file MyClass is in. Jun 27, 2012 at 21:24
  • 2
    I have just updated the answer. If it still doesn't work for you, then I'm at a bit of a loss. Jun 27, 2012 at 21:47
  • 2
    I have updated the answer again in response to the above two comments. @Divine, I would suggest reading up more on Extension Methods here: msdn.microsoft.com/en-us/library/bb383977.aspx Jun 27, 2012 at 22:06
10

Could someone please help me understand the concept of "Extending Sealed class" and its "Real time usage and benefits"

The basic concept is that a sealed class means you cannot subclass (extend) it. This is the entire reason the class is sealed - this "seals" the class so it cannot be extended.

Extension methods allow you to make a method that appears to extend the class, but is really a normal static method. The compiler calls this method, and passes the class instance.

1
  • Thank you so much Reed, I got my eyes open now.... Was blank on this when asked in an interview...He was trying to confuse me in interview :(
    – Jasmine
    Jun 27, 2012 at 20:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.