One of my projects has a / struct value type, which is a user-id string for the video format. In this case, it will contain a string of type content, but this can change.
I used a structure so that it could be heavily printed when it passed, and perform some health checks on the original value of the string.
public struct VideoFormat { private string contentType; public VideoFormat(string contentType) { this.contentType = contentType; } public string ContentType { get { return this.contentType; } } public override string ToString() { return this.contentType; }
As there are several very common formats, I expanded them as read-only static fields with default values.
public static readonly VideoFormat Unknown = new VideoFormat(string.Empty); public static readonly VideoFormat JPEG = new VideoFormat("image/jpeg"); public static readonly VideoFormat H264 = new VideoFormat("video/h264");
Is it better to set general values ββas read-only static fields or how to get only properties? what if i want to change them later? I see both methods used within the .Net framework, for example. System.Drawing.Color uses readonly static properties, while System.String has a static read field for String.Empty , and System.Int32 has a constant for MinValue .
(Mostly copied from this question , but with a more specific and not directly related question.)
source share