Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class design - what can I use instead of "static abstract"?

I want to do the following

public abstract class MyAbstractClass
{
    public static abstract int MagicId
    {
        get;
    }

    public static void DoSomeMagic()
    {
        // Need to get the MagicId value defined in the concrete implementation
    }
}

public class MyConcreteClass : MyAbstractClass
{
    public static override int MagicId
    {
        get { return 123; }
    }
}

However I can't because you can't have static abstract members.

I understand why I can't do this - any recommendations for a design that will achieve much the same result?

(For clarity - I am trying to provide a library with an abstract base class but the concrete versions MUST implement a few properties/methods themselves and yes, there are good reasons for keeping it static.)

like image 340
Ryan Avatar asked May 24 '10 19:05

Ryan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

You fundamentally can't make DoSomeMagic() work with the current design. A call to MyConcreteClass.DoSomeMagic in source code will be translated into MyAbstractClasss.DoSomeMagic in the IL. The fact that it was originally called using MyConcreteClass is lost.

You might consider having a parallel class hierarchy which has the same methods but virtual - then associate each instance of the original class with an instance of the class containing the previously-static members... and there should probably only be one instance of each of those.

like image 64
Jon Skeet Avatar answered Oct 18 '22 17:10

Jon Skeet


Would the Singleton pattern work perhaps? A link to the MSDN article describing how to implement a singleton in C#:

http://msdn.microsoft.com/en-us/library/ff650316.aspx

In your particular example, the Singelton instance could extend an abstract base class with your MagicId in it.

Just a thought :)

like image 24
Allen E. Scharfenberg Avatar answered Oct 18 '22 17:10

Allen E. Scharfenberg