Skip to main content

Sealed Class AND PARTIAL CLASS

 


Classes can be declared as sealed. This is accomplished by putting the sealed keyword before the keyword class in the class definition Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as sealed class, this class cannot be inherited. A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. Sealed classes are primarily used to prevent derivation. Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.
class Class1 { static void Main(string[] args) { SealedClass sealedCls = new SealedClass(); int total = sealedCls.Add(4, 5); Console.WriteLine("Total = " + total.ToString()); } } // Sealed class sealed class SealedClass { public int Add(int x, int y) { return x + y; } }
Sealed Method :
When an instance method declaration includes a sealed modifier, that method is said to be a sealed method. A sealed method overrides an inherited virtual method with the same signature. A sealed method shall also be marked with the override modifier. Use of the sealed modifier prevents a derived class from further overriding the method.
class A { public virtual void First() { Console.WriteLine("First Class A"); } public virtual void Second() { Console.WriteLine("Second Class A"); } } class B : A { public sealed override void First() { Console.WriteLine("First Class B"); } public override void Second() { Console.WriteLine("Second Class B"); } } class C : B { public override void Second() { Console.WriteLine("First Class C"); } }
Partial Class :
  • We were declaring a class in a single file but Partial class is a feature which allows us to write class across multiple files.
  • The partial indicates that the parts of the class, struct, or interface can be defined in the namespace. All the parts must be used with the partial keyword. All the parts must be available at compile time to form the final type or final class. All the parts must have the same accessibility level, such as public, private, protected, and so on.
  • If any part of the class is declared abstract, then the whole type is considered to be as abstract.
  • If any part is declared sealed, then the whole type is considered to be as sealed.
  • If any part declares a base type, then the whole type inherits that class.
Example: Test1.cs:- namespace PartialClass { public partial class MyTest { private int a; private int b; public void getAnswer(int a, int b) { this.a = a; this.b = b; } } } Test2.cs:- namespace PartialClass { public partial class MyTest { public void PrintCoOrds() { Console.WriteLine("Integer values: {0},{1}", a, b); Console.WriteLine("Addition: {0}", a+b); Console.WriteLine("Mulitiply: {0}", a * b); } } } Program.cs:- namespace PartialClass { class Program { static void Main(string[] args) { MyTest ts = new MyTest(); ts.getAnswer(12, 25); ts.PrintCoOrds(); Console.Read(); } } } OUTPUT:
Integer values: 12,25
Addition: 37
Mulitiply: 300
Advantages:
  • More than one Programmer or developer can simultaneously write the code for class.
  • Partial class allows a clean separation of business logic layer and the user interface.

Partial Method :
  • Partial class or struct can contain Partial method.
  • One part of class contains signature or declaration of the method and the implementation or definition of method can be in same class or different class.
  • Partial methods enable the implementer of one part of a class to define a method, similar to an event. The implementer of the other part of the class can decide whether to implement the method or not. If the method is not implemented, then the compiler removes the method signature and all calls to the method .
  • A partial method declaration consists of two parts: 1. definition and 2. Implementation.
  • partial void onNameChanged(); // Implementation in file2.cs partial void onNameChanged() { // method body }
  • Partial methods can have ref but not out parameters.
  • Partial method can have static or unsafe modifiers but can not be extern as presence of body decide whether they are defining or implementing.
  • Partial method can be Generic.
  • Partial methods are implicitly private, and therefore they cannot be virtual.

Comments

Popular posts from this blog

JQuery lightbox image slideshow gallery in asp.net

Introduction: In this article I will explain how to create lightbox image slideshow in asp.net using JQuery. Description:  In previous post I explained about  Ajax SlideshowExtender sample  to display images slideshow. Now in this article I will explain how to create lightbox image slideshow in asp.net using JQuery. In many websites generally we will see different types of slideshows like whenever we click on image that is automatically open with lightbox effect and we have a chance to see all the remaining images by press next or previous options in slideshow. All these Slideshows are implemented by using JQuery plugins. After seen those slideshows I decided to write post to use JQuery plugins to implement beautiful slideshown in asp.net.  To implement this one I am using  previous post  insert and display images from folder based on imagepath in database  because in that post I explained clearly how to insert images into our project folder a...

Scrollable GridView with Fixed Headers using jQuery Plugin

Using the same example I have created a jQuery Plugin for Scrollable GridView with Fixed header so that you can directly make a GridView scrollable.   HTML Markup < form   id ="form1"   runat ="server"> < asp : GridView   ID ="GridView1"   runat ="server"   AutoGenerateColumns   =   "false"> < Columns > < asp : BoundField   DataField   =   "ContactName"   HeaderText   =   "Contact Name"   /> < asp : BoundField   DataField   =   "City"   HeaderText   =   "City"   /> < asp : BoundField   DataField   =   "Country"   HeaderText   =   "Country"   /> Columns > asp : GridView > form >   Applying the Scrollable Grid jQuery Plugin < script   src ="Scripts/jquery-1.4.1.min.js"   type ="text/javascript"> script > < script   src ="Scripts/Scro...

SSRF CONFIGURTION

If this is your first time creating an SSRS report, then this is the right article for you. Below is a step-by-step guide that will help you to create, deploy and view your first report using SQL Server Reporting Services 2008: Environment Check Assuming the following are installed and already configured on your machine: Installed SQL Server 2008 R2 Installed AdventureWork Sample Databases (You can visit Microsoft website to download and install it: http://msftdbprodsamples.codeplex.com/releases/view/55926 We are using it in this case as a sample database. However, you can write any SQL statement on one of your pre-installed Databases) Installed SQL Server 2008 Reporting Services (If you have not done this, visit this post: SSRS 2008 Installation Guide ) Both SQL Server and Reporting Services are installed on the same machine (localhost) Now we can start the guide walk-through: 1. Open SQL Server Business Intelligence Development Studio ...