Private Static Members inside a Static Class... Good idea?

jramm

I created a static class with a private static member inside of it. That private static member is accessible to all of my static class methods.

This happened while I wasn't really paying attention, but then I realized what I had done, and that - interestingly enough - it seems to be working fine in my application. Nonetheless, it seemed like a silly thing to do (coming from C++), so I have been searching around trying to find more info on whether this is really supposed to be possible and/or if it is considered good or bad practice, but I haven't really been able to find anything at all about creating private static members inside a static class in C#.

It almost seems like static methods inside my static class have an implicit "this" variable (since I'm also able to call other methods without fully qualifying them with the class name), which feels a be peculiar to me.

I was hoping some of you might have some thoughts about whether this is a good idea or not, and why C# makes this possible at all.

The class:

public static class ControlHighlighter
{
    private static Panel highlightPanel = null;

    public static void Highlight(Control control = null, int thickness = 1)
    {
        RemoveHighlight();

        if (control != null)
        {
            if (control.Parent != null)
            {
                highlightPanel = new Panel();
                control.Parent.Controls.Add(highlightPanel);
                highlightPanel.Location = new Point(control.Location.X - thickness,
                                                    control.Location.Y - thickness);
                highlightPanel.Size = new Size(control.Size.Width + (2 * thickness),
                                               control.Size.Height + (2 * thickness));
                highlightPanel.SendToBack();
                highlightPanel.BackColor = SystemColors.Highlight;
            }
        }
    }

    public static void RemoveHighlight()
    {
        if (highlightPanel != null)
        {
            highlightPanel.Dispose();
            highlightPanel = null;
        }
    }
}
xxbbcc

In general, there's nothing wrong with having private static members in static classes (or even in non-static classes). They do pose some potential problems, though: when your application is multithreaded, these static members are shared across all threads so you have to apply locking around them.

Since you never know if you need to make your application multithreaded, it's best to keep the number of static variables to a minimum - all static variables shared between threads must be protected through locks or other synchronization primitives. It's far easier to do this kind of work ahead of time than patching problems later on.

However, in your specific example, you're putting a UI control in a static variable - this is something I'd definitely advise against. UI controls live on the UI thread and must be properly invoked when called from a different thread. Threading issues aside, putting a control in a static variable is a recipe for trouble - the static variable requires careful bookeeping to clean up - if the form hosting the control goes away, the static reference will still keep it in memory (because the control cannot go away). This can lead to all kinds of hard-to-find problems.

If you're not familiar with static in C#, I recommend reading MSDN for more detail.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

DLL class with a static members

分類Dev

Access method of private class inside static inner class from main()

分類Dev

Access method of private class inside static inner class from main()

分類Dev

Accessing static members of a class within static methods

分類Dev

Java Classloaders - Invoking a static method inside a private class

分類Dev

Lifetime of static class members/class references?

分類Dev

C# static member class with members

分類Dev

unit test for private static method in non-static class

分類Dev

Alternatives to static members

分類Dev

Are non-static class members destroyed even without a destructor?

分類Dev

How to populate static members of recurring template derived class?

分類Dev

Doesn't static class members have no association with the this pointer?

分類Dev

Is import static a good practice?

分類Dev

(Why) can we assign non-static class members to static variables in initialization?

分類Dev

Initializing a static map using static members

分類Dev

Is there a Necessity for private static Methods?

分類Dev

Typescript access to private static fields within same class

分類Dev

Accessing a method of the Outer Class inside a static Inner Class

分類Dev

static class initialization with static references

分類Dev

Private members of class - within this context

分類Dev

Generic static method inside usual class; uses array of basic type

分類Dev

How do I use a static variable inside a class in Python

分類Dev

Static class stored as a property

分類Dev

Declaring static class variables

分類Dev

Python Static Class attributes

分類Dev

static class self reference

分類Dev

Static members of classes used by Azure functions

分類Dev

Static Cast to access static const class member

分類Dev

How to access private class's property from public static method in PHP

Related 関連記事

  1. 1

    DLL class with a static members

  2. 2

    Access method of private class inside static inner class from main()

  3. 3

    Access method of private class inside static inner class from main()

  4. 4

    Accessing static members of a class within static methods

  5. 5

    Java Classloaders - Invoking a static method inside a private class

  6. 6

    Lifetime of static class members/class references?

  7. 7

    C# static member class with members

  8. 8

    unit test for private static method in non-static class

  9. 9

    Alternatives to static members

  10. 10

    Are non-static class members destroyed even without a destructor?

  11. 11

    How to populate static members of recurring template derived class?

  12. 12

    Doesn't static class members have no association with the this pointer?

  13. 13

    Is import static a good practice?

  14. 14

    (Why) can we assign non-static class members to static variables in initialization?

  15. 15

    Initializing a static map using static members

  16. 16

    Is there a Necessity for private static Methods?

  17. 17

    Typescript access to private static fields within same class

  18. 18

    Accessing a method of the Outer Class inside a static Inner Class

  19. 19

    static class initialization with static references

  20. 20

    Private members of class - within this context

  21. 21

    Generic static method inside usual class; uses array of basic type

  22. 22

    How do I use a static variable inside a class in Python

  23. 23

    Static class stored as a property

  24. 24

    Declaring static class variables

  25. 25

    Python Static Class attributes

  26. 26

    static class self reference

  27. 27

    Static members of classes used by Azure functions

  28. 28

    Static Cast to access static const class member

  29. 29

    How to access private class's property from public static method in PHP

ホットタグ

アーカイブ