Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 12
#DOTNET 10
#LINQ 6
LINQ 5
#ASP.NET 4
#DATAACCESS 4
ASP.NET 3
DOTNET 3
.NET 2
WCF 2

Archive · View All
July 2010 5
August 2010 4
June 2011 3
October 2010 1

How to add method in Sealed class

Jul 15 2010 9:53PM by IBhadelia   

In Object Oriented world Sealed class is class which can not inherited, its sealed not more implementation get added. This was correct before C#3.0 invented. C#3.0 Comes with lots of new functionality like Extension Methods, Anonymous Types, Partial Methods, Automatic Properties and Object-Collection Initialization. Also there is major portion added to C# 3.0 which is LINQ, you can find Introduction of LINQ here.

So, Sealed class; if we want to add method to any of class then the best way to achieve is to inherit that class and add your own method and going forward you use new derived class all over place. But in case of Sealed you can not do as you cant inherit Sealed class.

To achieve this, we can  use one of the feature of C#3.0, which is Extension Method. Let see with example, following is our Sealed class.

public sealed class User
{
    //Public properties
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

    //Some of the methods having implementation
    public bool SaveUser() { return true; } 
    public bool GetUser() { return true; }
}

Here User is class marked with sealed, which contains few properties and methods operated on it. If we try to inherit User class

public class MyUser : User
{ 
    
}

It will give you error saying MyUser cannot derived from sealed type ‘User’. Well this is normal behavior if should not allow you to inherit sealed class. But our goal to add new method to Sealed class, so let’s go in that direction.

We are going to use Extension Method added in C#3.0; extension methods are static methods that can be invoked using instance method syntax. The first step of creating extension method is to have static class in which our extension will going to resides. The method which we want is to return Full Name in some specific format. So lets create class and add our method into it.

public static class Extensions
{
    public static string GetFullName(this User usr)
    {
        return string.Empty;
    }
}

Syntax is little odd then normal method, there are few rules to define your extension method.

  1. It should resides in static class
  2. Extension method should be static
  3. First argument of extension method should be type on which you want to add method with this keyword

Calling this method is same as other method call, also it’s easy to identify our method as extension method, will see soon. Now lets add some implementation and code to call that method.

User user = new User()
{
    FirstName = "Nick",
    LastName = "Althoff",
    Age = 32,
    UserId = 22
};

Console.WriteLine(user.GetFullName());

And here is output

image

So, finally we have added method to Sealed class using Extension method. Now let see how we can identify which are the extension methods.

image

You can see there is small change in method icon of extension method.

You can add more parameters as required and also its recommended that extension methods be used only in situation where instance methods are not feasible or possible. Extension members of other kinds, such as properties, events, and operators, are being considered but are currently not supported.

Tags: OOPS, C#3.0, ibhadelia, #CSHARP, #DOTNET, BRH,


IBhadelia
164 · 1% · 299
1
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"How to add method in Sealed class" rated 5 out of 5 by 1 readers
How to add method in Sealed class , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]