prosseek
prosseek

Reputation: 190709

Global variables or equivalent in C#

I need to read XML file and store all the info inside variables/class when the software starts, and make the variables/classes be accessible every class.

With C++, I could use global variables or classes, even though it's not a good practice to have a global variables.

  1. If the variables/classes are read only data, what methods can I use for my purpose with C#?
  2. What if the variables/classes are read/write?

Upvotes: 2

Views: 726

Answers (3)

Jean-Bernard Pellerin
Jean-Bernard Pellerin

Reputation: 12670

static class with static public variables
read/write can be done this way:

private int _someInt;
public int SomeNumber{
  public get {return _someInt;}
  private set {_someInt = value;}
}

Upvotes: 2

moi_meme
moi_meme

Reputation: 9318

a static class with a static readonly variable

Upvotes: 5

Tim Barrass
Tim Barrass

Reputation: 4939

You could use a public static class, and make everything available from there. You could populate it with data at creation time. It won't technically be at global scope, but would make the data easily available, and you wouldn't explicitly have to instantiate anything.

Upvotes: 9

Related Questions