Click here to Skip to main content
16,020,347 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am trying to make a sealed class from following codes. But not understanding how to add functionality to by extending the the class using a static class with static members (functions, properties, or indexers). I am not getting any errors but, I don't think it is a proper sealed class.

{
   sealed class Magic
    {
        private int toads;
        private int spiders;

        public Magic()
        {
            toads = 0;
            spiders = 0;
        }

        public int Toads
        {
            get { return toads; }
            set { toads = value; }
        }
        public int Spiders
        {
            get { return spiders; }
            set { spiders = value; }
        }
        public Magic(int t, int s)
        {
            Toads = t;
            Spiders = s;
        }
        public void doMagic()
        {
            Console.WriteLine("Poof!");

        }
      }
    static class ExtendMagic 
    {
      
    }
    class Program
    {
        static void Main()
        {
            Magic yourMagic = new Magic();
            Console.WriteLine("Spiders = {0} and Toads = {1}", yourMagic.Spiders, yourMagic.Toads);
            Magic mymagic = new Magic(3, 5);
            mymagic.doMagic();
            Console.WriteLine("Spiders = {0} and Toads = {1}", mymagic.Spiders, mymagic.Toads);
            Console.ReadKey();

        }
    }
}
Posted

When you use the sealed keyword on a class, it prevents any class being derived from it; you can't do this:
C#
public sealed class A {}
public class B : A {}
It doesn't prevent extension methods being declared because they do not derive from the class, but extend it.
Extension methods do not get any additional access to the class internals, they just allow a syntactic sugar so you can add methods that look like part of the original class. For example, the DateTime class does not have a method to return the first of the month, so you can do this:
C#
DateTime firstOfMonth;
DateTime now = DateTime.Now;
firstOfMonth = (now.AddDays(1 - now.Day)).AtMidnght();

Or include one:
C#
/// <summary>
/// Returns the first day of the month
/// </summary>
/// <example>
/// DateTime firstOfThisMonth = DateTime.Now.FirstOfMonth;
/// </example>
/// <param name="dt">Start date</param>
/// <returns></returns>
public static DateTime FirstOfMonth(this DateTime dt)
    {
    return (dt.AddDays(1 - dt.Day)).AtMidnight();
    }
Now your code can use the clearer format:
C#
DateTime firstOfMonth = DateTime.Now.FirstOfMonth();
 
Share this answer
 
The question makes no sense and looks more than pointless. This is a properly sealed class. You did not demonstrate extending this class, did not even try; and you cannot do so.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900