Bill Gates
Bill Gates

Reputation: 857

Global config object

I want to initialize kind of global object, which I can refer anytime and from any part of my application. I don't want to pass it from one method to another down the execution process, I just want to read all information once from my config file and store it into object and when use that information any time I need from any place. My problem is that not sure about how to declare that class technically to be accessible and won't cleaned by garbage collector. Could you please provide a code example of such "Global class".

Looking for your advice. Thanks!

Upvotes: 1

Views: 1358

Answers (1)

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68687

You could use a singleton to declare it. Here are some sample implementations. I personally prefer to use a static class with properties

public static class Config
{
    public static string IP { get {...

Remember, as long as anything has a reference to your instance, it won't be garbage collected.

Upvotes: 3

Related Questions