c#中主要方法的泛型类 [英] Generic Class With Main Method in c#

查看:49
本文介绍了c#中主要方法的泛型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有错误可以解释某些原因我会非常感谢你。



Why there is error can some one explain some reason i will be very thankful to you .

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)
        {

        }
    }
}









错误是





错误1程序GenericWithMainMethod \obj\ x86 \Debug \GenericWithMainMethod.exe'不包含适用于入口点的静态'Main'方法GenericWithMainMethod





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

推荐答案

你需要记住的关于.net中泛型类的一件事是,在你提供类型之前它们才真正存在 - 制作一个具体的类。



目前,编译器没有足够的信息。即使访问静态成员(与Main一样),也必须指定类型。



泛型类型中静态成员的一个重要事项是它们是由同一类型的不同具体类型共享。在你的情况下,这意味着运行时无法调用Main。





看看这个:

错误CS0402:警告错误:'GenericWithMainMethod.Program< t> .Main(string [])':入口点不能是通用的或通用类型



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





这可能(或可能不)帮助理解:



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:

# 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?


你忽略了这样一个事实:对于.NET控制台应用程序和WinForms应用程序,一个名为'Program ...的类不是generic ...是一个特殊的结构,用于告诉编译器如何启动应用程序。



如果是使用File / New在Visual Studio中创建的控制台应用程序/ Project =>选择ConsoleApplication,程序类不标记为'静态,'主要写入:
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:
class Program
{
    static void Main(string[] args)
    {
    }
}

使用File / New / Project =>创建新的WinForm项目时选择Windows窗体应用程序:

When you create a new WinForm Project using File/New/Project => select Windows Forms Application:

static class Program
{
    [STAThread]
    static void Main()
    {
    }
}

可以从WinForms程序类中删除'静态修饰符,但仍然有一个工作程序。



如果是控制台应用程序,你可以将程序类的声明更改为'静态,它仍然会运行。



但是,我看不出任何理由为什么有人想要将WinForms应用程序'Program Class的默认声明修改为非静态,或者将Console应用程序类修改为静态!



你创建一个通用程序<>上课只是......一个错误。并且,在这种情况下,您不使用您指定的通用类型。



您在这里真正想要实现的目标是:在程序中添加一种方法class?

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 ?


这篇关于c#中主要方法的泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆