tags:

views:

287

answers:

9

when i prefer static or normal class? Or what is the difference between them?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace staticmethodlar
{
    class Program
    {
        static void Main(string[] args)
        {
            SinifA.method1();
        }
    }

    static class SinifA 
    {
       public static void method1()
        {
            Console.WriteLine("Deneme1");
        }
    }

    public static class SinifB
    {
        public static void method2()
        {
            Console.WriteLine("Deneme2");
        }
    }
    public class sinifC
    {
       public void method3()
        {
            Console.WriteLine("Deneme3");
        }
    }

   public class sinifD : sinifC
    {
        void method4()
        {
            Console.WriteLine("Deneme4");
        }

        sinifC sinifc = new sinifC();  // i need to use it:)


    }
}
+4  A: 

From the MSDN documentation on static classes:

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

R0MANARMY
one wonders what all those GB of docs are for.
Anders K.
Was the "Learn to read documentation" comment really necessary? Sure, I will answer your question but also show you what an intellectually superior @ss I can be. Happy Friday!
Scott
@WaggingSiberian: Giving him/her the benefit of the doubt, that's the first Google result for search phrase **static class C#**, yes it was necessary. I don't feel like doing a google search before posting a question is an unreasonable requirement.
R0MANARMY
@romanarmy, if answering questions that are so simple for you require you to post snarky comments, why waste your time? Move along, let someone that can answer the question, without being an jerk, post an answer. You could have simply posted the Google link and let it go. Sometimes it's all in the way you present it.
Scott
+2  A: 

You can create instances of "normal" classes via the class constructor.

var normal = new Normal();

You cannot create instances of static classes. They can only have static methods.

Also worth noting is that you must declare extension methods in static classes.

John Bledsoe
ok when i use static class?
Phsika
Good point about extension methods.
R0MANARMY
A: 

Static members can be called without using an instance of the class. For example, a static Math class with an Area method call be called by Math.Area without instantiating the Math class first.

Scott
+1  A: 
  • static classes cannot be instantiated or inherited.
  • static classes are marked as sealed and abstract by compiler in the output MSIL.
  • all members of static classes must be static as well.
  • only static classes can host extension methods.
  • static classes cannot be used as generic type arguments.
this. __curious_geek
+1  A: 

In simple terms:

A static class cannot be instantiated using the new keyword. All methods in a static class must be static and can be called directly without instantiation.

A "normal" class MUST be instantiated however any static methods in a "normal" class can be called without instantiation.

Non static methods in a "normal" class cannot be called without first instantiating the containing class.

Sani Huttunen
+4  A: 

Static classes contain static objects that can't be instantiated multiple times. Usually what I use static classes for are to house static methods that provide calculations, general processing patterns, string output formats, etc. Static classes are light weight and don't need instantiation.

For instance System.IO.File is a static class with static a method Exists(). You don't create a File object to call it. You invoke it like this

System.IO.File.Exists(filePath)

Rather than doing this

System.IO.File myFile = new System.IO.File(filePath);

if(myFile.Exists()) { /* do work */ }

If you require several objects in software, then you use dynamic classes. For instance if you have an inventory system you may have several Product objects and in that case you would use a dynamic class such as this

public class Product
{

    private int ProductID;
    private string ProductName;
    private int Qty;

    public Product( int productID, string productName, int total )
    {
        this.ProductID = productID;
        this.ProductName = productName;
        this.Qty = total;
    }

    public int GetQty()
    {
        return Qty;
    }
}
jlafay
A: 

A static class is one that cannot be instantiated. Therefore, it cannoot be used as a 'template" for multiple instances of some object that conforms to the class definition as a normal class can. In a normal class, the data members defined are created for each "instance" of the class, and each "instance" has it's own set of these data members, so these instances represent individual "entities" or complex in-memory data structures for some object (which you coded the class to represent).

When you define (code) a static class, all of it's data fields must also be static, meaning that they cannot be instantiated once for each instance of the class, (you can;t create instances of a static class ). Static classes are useful only when you just need a container to hold methods that do not depend on the state of some object or data structure being managed by the application (like an add function, or a string formatter, etc.)

Charles Bretana
A: 

A normal class is one which you can instantiate and can use with objects.

A static class is one which can not be instansiated and can't be extended.

That means a static class is sealed and abstract by default, you may look at the

MSIL of a static class compiler puts sealed and abstract in front of a static class.

So, because a static class can't be instantiated, you can't have instance methods defined

on static classes, so all methods must be static and public ofcourse as you would want to

use them.

A static class is singleton by default.

Static classes are used for defining extension methods because you don't want to

instansiate them.

Static classes are like global provider of unique services.

I hope that make sense to you and will help you to understand the static classes.

Praveen
A: 

It might help to think of "static" and "instance" in terms of memory addressing.

An object that is static exists in one and only one location in memory; you can't create instances of it because by definition it has been stated that it will be found in one particular location. It is "static" and unchanging. The creation of the object is implicit rather than explicit.

var result = Math.Pow(3, 3); // static - Math class can't be instanced

An object that isn't static can be instanced one or more times; you're saying, "Create for me an object of this type" and the object is given a memory address. You can create multiple instances that all exist in different memory addresses. The creation of the object is explicit using the new keyword.

var Ted = new Employee(); // instance - creates a new object at a new address

This is where people get into trouble in ASP .NET web programming; static classes are created once per Application Domain and their constructors are therefore only called once.

Anachronistic