Hey guys,
does anybody know an attribute that would protect a function from being overwritten by the dataset custom tool code generator?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Niradhip ChakrabortyPosted Apr 15, 2008, 4:33 PM
Sealed Classes And Methods In C#
The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class.
The sealed modifier is primarily used to prevent unintended derivation, but it also enables certain run-time optimizations. In particular, because a sealed class is known to never have any derived classes, it is possible to transform virtual function member invocations on sealed class instances into non-virtual invocations.
In C# structs are implicitly sealed; therefore, they cannot be inherited.
using System; sealed class MyClass { public int x; public int y; } class MainClass { public static void Main() { MyClass mC = new MyClass(); mC.x = 110; mC.y = 150; Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); } }In the preceding example, if you attempt to inherit from the sealed class by using a statement like this:
class MyDerivedC: MyClass {} // Error
You will get the error message:
'MyDerivedC' cannot inherit from sealed class 'MyBaseC'.
In C# a method can't be declared as sealed. However when we override a method in a derived class, we can declare the overrided method as sealed as shown below. By declaring it as sealed, we can avoid further overriding of this method.
using System; class MyClass1 { public int x; public int y; public virtual void Method() { Console.WriteLine("virtual method"); } } class MyClass : MyClass1 { public override sealed void Method() { Console.WriteLine("sealed method"); } } class MainClass { public static void Main() { MyClass1 mC = new MyClass(); mC.x = 110; mC.y = 150; Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); mC.Method(); } }Niradhip ChakrabortyPosted Apr 15, 2008, 3:19 PM
U can make the function as virtual override it.
class Foo
{
public virtual void getName()
{
Console.WriteLine("Foo!");
}
}
class Zorb : Foo
{
public override void getName()
{
Console.WriteLine("Zorb!");
}
}
class Program
{
static void Main(string[] args)
{
// Simple foo declaration
Foo foo = new Foo();
foo.getName();
// Polymorphism?
Foo foo2 = new Zorb();
foo2.getName();
//Output is:
// Foo!
// Zorb!
}
}
Try messing around with the above code and see which produces the answer you want. Only by overwriting Foo's virtual getName method, will I be able to print out
Zorb! In the second example where Foo foo2 = new Zorb(), if I don't declare getName() as having been overridden in Zorb, the output would actually be Foo! Even using the new keyword instead of override will result in Foo! being displayed.
So dont use virtual or make the method as private to protect it from
overrideing.