6

I need to use a static class to hold some simple values during runtime:

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

I now have need to store a List of objects - typed according to classes I have defined. This List will be re-used for different Object types, and so I thought to define it as Generic.
This is where I am stuck: how do I define a property inside a static class that will be containing different types of Lists at different times in the applications execution?

Needs to be something like :

 public List<T> Results {get; set;} 
1
  • 2
    Since your class is named Globals, it seems like you're heading down a wrong path here. What do you need the list to contain, and why should it be generic? Commented Dec 14, 2012 at 9:15

1 Answer 1

10

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 property Results needs to be private. You may consider exposing your methods to interact with the list, instead of exposing the list through a property.

See: C#/.NET Fundamentals: Returning an Immutable Collection

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome stuff! Thanks Habib!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.