Monday, October 15, 2007

Overriding static member in child class with C#.NET,VB.NET

Static member are accessible without creating the objects of the class so you don't need to create a object for that class.

Micorosft CLR does not permit the static methods to be override in child class. But off course you can hide the static method of parent method with new keyword in child class. Here is the example of that.


public class A
{
public static virtual void Test()
{
}
}
public class B : A
{
public new override static void Test()
{
}
}
Share:

3 comments:

  1. I just saw this code and tried it. This is the message "Error 1 A static member 'ConsoleApplication1.A.Test()' cannot be marked as override, virtual, or abstract D:\SKS\AffinityMobile\Works\ConsoleApplication1\Program.cs 51 36 ConsoleApplication1
    "

    Please explain.

    ReplyDelete
  2. Get rid of "virtual" in class A's method declaration, and it should work. The concept is still valid.

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Search This Blog
About Me
My Photo

Full Stack Developer, Blogger, Microsoft Mvp, Mentor, Lifelong learner, 12+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC/Core, C#, Angular,Vue.js and node.js
Subscribe to my blog

  

My You tube channel- Please Subscribe
Youtube Video Playlist
My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews