Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was this class instantiated inside itself?

Tags:

c#

Please help me understand this code. Is this like an enumeration with objects instead of values? Is there are term or pattern that explains this process?

public class State
{
    private State(String value)
    {
        Value = value; 
    }

    public String Value { get; set; }

    public static State Open => new State("Open");

    public static State Closed => new State("Closed");

    public static State YourOpen => new State("YourOpen");

    public static State YourClosed => new State("YourClosed");
}
like image 272
Mike Murphy Avatar asked Jan 28 '23 04:01

Mike Murphy


1 Answers

It seems to be a class that:

  1. allows to create instances with custom status values (not publicly accessible!)
  2. provides instances of itself with pre-defined values

I had overlooked the private constructor. As @jacob-krall pointed out the typesafe enum (coming from older Java) seems to suit best.

like image 94
casiosmu Avatar answered Feb 06 '23 10:02

casiosmu