Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Why there is error can some one explain some reason i will be very thankful to you .

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

namespace GenericWithMainMethod
{
    class Program<T>
    {
        public void Dispaly() {

            Console.WriteLine("Why its happen.");

        }
        static void Main(string[] args)
        {

        }
    }
}




THE ERROR IS


Error 1 Program GenericWithMainMethod\obj\x86\Debug\GenericWithMainMethod.exe' does not contain a static 'Main' method suitable for an entry point GenericWithMainMethod
Posted
Updated 29-Oct-14 18:52pm
v2
Comments
Laiju k 30-Oct-14 0:48am    
what was the error be specific
siddharth629 30-Oct-14 0:52am    
Error 1 Program 'GenericWithMainMethod\GenericWithMainMethod\obj\x86\Debug\GenericWithMainMethod.exe' does not contain a static 'Main' method suitable for an entry point GenericWithMainMethod
PIEBALDconsult 30-Oct-14 0:48am    
Well? What error?
siddharth629 30-Oct-14 0:53am    
Error 1 Program 'GenericWithMainMethod\GenericWithMainMethod\obj\x86\Debug\GenericWithMainMethod.exe' does not contain a static 'Main' method suitable for an entry point GenericWithMainMethod
Laiju k 30-Oct-14 1:15am    
static void Main(string[] args)
{

}
remove this from code

One thing you need to remember about generic classes in .net is that they don't really exist until you provide the type -- to make a concrete class.

As it stands, the compiler doesn't have enough information. Even to access static members (as with your Main), you must specify the type.

One of the important things about static members in generic types, is that they are not shared by different concrete types made from the same generic type. In your case, that means the runtime has no way to call Main.


Have a look at this:
error CS0402: Warning as Error: 'GenericWithMainMethod.Program<t>.Main(string[])': an entry point cannot be generic or in a generic type

http://msdn.microsoft.com/en-us/library/ht5h7yhs(v=vs.90).aspx[^]


This might (or might not) help understand:

XML
# pragma warning disable 0402

namespace GenericWithMainMethod
{
    class Program<T>
    {
        private static System.Type type = typeof(T) ;

        public void Dispaly()
        {
            System.Console.WriteLine("I am a {0}" , type );
        }

        /* this causes warning 0402 */
        public static void Main(string[] args)
        {
          (new Program<T>()).Dispaly() ;
        }
    }

    class C
    {
        static void Main(string[] args)
        {
          Program<int>.Main(null) ;
          Program<string>.Main(null) ;

          System.Console.ReadLine() ;
        }
    }
}




What were you trying to do?
 
Share this answer
 
v4
Comments
siddharth629 30-Oct-14 0:56am    
sir i want to call Dispaly() Method Within same class.
PIEBALDconsult 30-Oct-14 1:02am    
Dispaly is not static, so you need an instance.

But that's a separate issue.
siddharth629 30-Oct-14 1:07am    
sir ,can we call Main method in Generic class. how ?
PIEBALDconsult 30-Oct-14 1:10am    
You can write code that does, but that won't help, the runtime has no way to call it as the process' an entry point.
Sergey Alexandrovich Kryukov 30-Oct-14 2:12am    
5ed. Pretty good explanation and nice work-around code (I don't understand why though, but the point is to give OP an idea on what's going on).
—SA
You are ignoring the fact that for both .NET Console Applications, and WinForms Apps, a Class named 'Program ... which is not generic ... is a special construction used to tell the compiler how to start the Application.

In the case of a Console Application created in Visual Studio using File/New/Project => select ConsoleApplication, the Program Class is not marked 'static, and 'Main is written:
C#
class Program
{
    static void Main(string[] args)
    {
    }
}
When you create a new WinForm Project using File/New/Project => select Windows Forms Application:
C#
static class Program
{
    [STAThread]
    static void Main()
    {
    }
}
It is possible to remove the 'static modifier from the WinForms Program Class and still have a working program.

In the case of a Console App you can change the declaration of the Program Class to 'static and it will still run.

However, I cannot see any reason why anyone would ever want to modify the default declarations of a WinForms app 'Program Class to non-static, or a Console app Program Class to static !

Your creating a generic Program<> class is just ... a mistake. And, in this case, you make no use of the generic Type you specify.

What were you really trying to achieve here: to add one method to the 'Program class ?
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 30-Oct-14 2:15am    
5ed. "Just a mistake" is a good point. The "generic" idea looks pointless to me.
Why did you mean static class declaring Main? It's pretty much irrelevant. This class can be static or not static, depending on how it is supposed to be used beyond the static Main.
—SA
BillWoodruff 30-Oct-14 3:28am    
Thanks for the feedback, and the vote: I have edited the post to, I hope, more clearly distinguish between what Visual Studio generates, by default, when you create a new Console App, or WinForms app, and what is "legal" to change, and what cannot be changed.

And, to urge the OP not to change things just because they can :)
Sergey Alexandrovich Kryukov 30-Oct-14 11:47am    
Aha, that's good...
—SA
PIEBALDconsult 30-Oct-14 9:33am    
There is absolutely no reason to name the class "Program".
Sergey Alexandrovich Kryukov 30-Oct-14 11:47am    
Of course you are right. I just did not understand who do you think insisted on using this exact name, where?
Thank you.
—SA

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