C# .NET - why we create static and abstract class

Asked By myzonal.com myzonal.com on 02-Aug-13 05:34 AM
Hi All,
 
Let me know why we create static and abstract class.
we know that we can't instantiate static class and is very fast
so why we not create every class as static class..
why we create objects of public class.
 
when we need static class when abstract class and when public class.....
Robbe Morris replied to myzonal.com myzonal.com on 02-Aug-13 08:33 AM
You use a static class typically when you only need one instance of it for every user in the app.  Perhaps it contains mostly business logic.  There are numerous times when you need to create instances of a class.  List<Person> is an example.  If you have a list of people, you'd need a separate instance of each Person class.  So, these could be static.

Public classes are for when you want to enable every other class and assembly in your application (and external applications using those assemblies) to be able to use the class and create instances of it.  If you only want instances of the class created from your own code inside a given assembly, you'd use something like "protected" or perhaps even "private".  You do this to simply the interface of the class exposed to code trying to use it.  Either for security or just to prevent use of it by mistake.

You might want to pick up a good Object Oriented Programming book for C#.