Click here to Skip to main content
15,887,368 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here object is created by two different type one bye calling the method which is returning the static instance one via new keyword . is there is difference between these two object and what is the reason behind it
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Program
{
    private static Program instance = new Program();

    public static Program GetInstance()
    {
        return instance;
    }

    public Program Msg()
    {
        Program p1 = new Program();
        Console.WriteLine("HII");
        return p1;
    }
}
class Class2 : Program
{
    private static void Main(string[] args)
    {
        Program program = new Program();  //  here the object( base class) is create  by simple new Key word
        program.Msg();

        Program program1 = Program.GetInstance();// here the object is create by GetInstance() method which return static
        program1.Msg();
        Console.Read();
    }
}
Posted
Updated 19-Sep-13 19:48pm
v2

To me your questions, and your code, suggest that you really need to go back and study the differences between Classes declared as 'static, and declared as 'public, and the requirements/implications of the use of Constructors in both types of Classes.

You may find examining Jon Skeet's many thoughtful posts on use of 'static Classes on StackOverFlow helpful: [^], such as this one: [^].

What are your goals here ? If what you are trying to implement is a 'Singleton pattern class, this article by Skeet on his blog may be helpful: [^].

If you are new to .Net and C#, I'd recommend studying implementations of 'public and 'static Classes separately, rather than mixing them together as shown in your code. I have yet to see a real-world scenario in which a Class defined as non-static, needs to define another, 'static, version of itself in code.

The convention in WinForms .NET, or a Console Application, that the Application itself is "launched" automatically (as long as the compiler has a valid reference) by accessing a static method ('Main) in a static Class ('Program), is a special case, and not something that you want to try and use as a model to build your own Application "super-structure" around.

Note that in your code the Class 'Program is not 'static, but 'public.
 
Share this answer
 
You obviously are trying to use the singleton pattern.
So you definitely should not write
C#
Program program = new Program();

This defeats the design pattern you tried to set up.
Just use
C#
Program program = Program.GetInstance();

There is no point in using a second variable of type Program.

Or: you definitely need several instances of your Program class, in that case the singleton pattern is irrelevant and should be removed from the definition of your class.

[EDIT]
When I come to use the singleton pattern, I always use the one describe in the following link:
Implementing the Singleton Pattern in C#[^]
[/EDIT
 
Share this answer
 
v2
As I know that -
C#
1 Program program = new Program();

This object creation will always create a new instance of Program class..
C#
2. Program program1 = Program.GetInstance();

This object creation will create only one instance of Program Class and its value is same in whole application. This type of object creation is known as Singleton Object.

For more information refer this link ---
http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]
 
Share this answer
 
v4
When using the singleton design pattern you have to take also into account the issues related with multithreading so read carefully what is presented in the link[^] given by Punam1990[^] in the previous solution.

Take a look also to this article here (http://csharpindepth.com/Articles/General/Beforefieldinit.aspx[^]). It gives some good information about some other issues you may find when using static constructors and static initializers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900