Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sealed-Partial Class

Tags:

c#

Can you make a partial class file for a class that is sealed?

like image 442
Gerard Avatar asked Aug 25 '10 19:08

Gerard


People also ask

What is sealed partial class?

A Sealed class is a class that cannot be inherited. A partial class is a class that can be split between 2 or more source files.

What are sealed classes?

Sealed classes and interfaces represent restricted class hierarchies that provide more control over inheritance. All direct subclasses of a sealed class are known at compile time. No other subclasses may appear outside a module within which the sealed class is defined.

What are partial classes?

A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword.

What is difference between sealed and static class?

A Sealed class cannot be inherited from and can static and non-static members or methods. Contains a public constructor. 2. A Static class cannot be inherited from and contains only static methods and class properties.


2 Answers

The sealed keyword simply means that the class cannot be inherited. It has no impact on how the class' code is structured otherwise. The partial keyword simply allows a class to be split among several files.

In the sample below, class A compiles just fine. B does not compile because A is sealed and inheritance is not allowed.

public sealed partial class A   { private int x; }

public sealed partial class A   { private int y; }

public class B : A  {   }
like image 127
Paul Sasik Avatar answered Nov 15 '22 19:11

Paul Sasik


It seemed to compile fine.

sealed partial class Class1
{
    public void MyMethod() { }
}

partial class Class1
{
    public void MyMethod2() { }
}  
like image 43
James Curran Avatar answered Nov 15 '22 19:11

James Curran