-1

While learning about static classes, I went through many forums as per my understanding I got to know that static class members can be accessed directly using class name and hence not required to create object of that class which in turn helps save memory and faster execution of program.

So my question is if this is the case then why not always use static class over normal class.

I know my question may be little weird, also I may be wrong with the concepts. Please if any one can explain this in detail with example. would be great help.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • You might want to create multiple version of the same object, for example, if you wanted to model a classroom each student would need their own object, unless it was a classroom full of clones with all the same name and stats :O –  Oct 03 '18 at 10:04
  • if you use a static class over an instanced class, then you're locked into only ever having one instance of the class. Say for example you're making an application that catalogues cars, if you have a class type of Car that you want to have a collection of, you will only be able to store 1 car as the data type is static. – Dan Scott Oct 03 '18 at 10:04
  • This might help https://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp – apomene Oct 03 '18 at 10:05

1 Answers1

0

A static class can not be instantiated. E.g. you can't use new to create an object of that class in C#.

There are many situations where you might want to create multiple objects of a class.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Slaytr
  • 1
  • 2
  • Welcome to stackoverflow. You just explained that static classes cannot be instantiated. But you didn't answer the questions "what is the purpose of static classes?" – Robert Oct 03 '18 at 12:38