I use more than one class and I need a... lets say Global storage for all the class and method. Is it the right way to create a static class for storage?
public static class Storage
{
public static string filePath { get; set; }
}
Or is there other ways to do it?
Applying Singleton to your original class:
usage:
You should have a look at the repository pattern:
http://martinfowler.com/eaaCatalog/repository.html
One way of implementing this pattern is through the use of ORM's s.a. NHibernate:
https://web.archive.org/web/20110503184234/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/10/08/the-repository-pattern.aspx
I love seeing that implementation of singleton in C#.
C# guarantees that your instance wont be overriden and your static constructor guarantees that you WILL have your static property instantiated before the first time it's used.
Bonus: It's threadsafe as per language design for static constructors, no double checked locking :).
If you really need to make your example a singleton then here is how you do it.
The way to call and set the singleton is the following:
Alternatively you can add into the constructor the following to avoid null reference exception:
Word of warning; making anything global or singleton will break your code in the long run. Later on you really should be checking out the repository pattern.
You could consider using the Singleton design pattern: Implementing Singleton in c#
eg.