Define a Generic List property in a static class

I need to use a static class to store some simple values ​​at runtime:

public static class Globals { public static string UserName { get; set; } ...more properties } 

Now I need to save the list of objects introduced according to the classes I defined. This list will be reused for different types of objects, so I decided to define it as Generic.
Here's where I got stuck: how to define a property inside a static class that will contain different types of lists at different runtimes of applications?

You need to be something like:

  public List<T> Results {get; set;} 
+6
source share
1 answer

You need to define your class as a Generic class.

 public static class Globals<T> { public static string UserName { get; set; } public static List<T> Results { get; set; } } 

later you can use it like:

 Globals<string>.Results = new List<string>(); 

Also, your Results property should be private. You might want your methods to interact with the list rather than expanding the list through a property.

See: C # /. NET Basics: returning an immutable collection

+10
source

All Articles