繁体   English   中英

不包含适用于入口点的静态main方法

[英]does not contain a static main method for suitable entry point

不断收到错误消息,其中不包含适用于切入点的静态main方法。 任何人都可以向我解释此错误,并可能帮助我解决该错误? 谢谢。 C#的新手

 {
    class Authenticator
    {

        private Dictionary<string, string> dictionary = new Dictionary<string, string>();

        public void IntialValues()
        {
            dictionary.Add("username1", "password1");
            dictionary.Add("username2", "password2");
            dictionary.Add("username3", "password3");
            dictionary.Add("username4", "password4");
            dictionary.Add("username5", "password5");
        }

        public bool Authenticate(Boolean authenticated)
        {
            Console.WriteLine("Please enter a username");
            string inputUsername = Console.ReadLine();

            Console.WriteLine("Please enter your password");
            string inputPassword = Console.ReadLine();

            if (dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword)
            {
                authenticated = true;
            }
            else
            {
                authenticated = false;
            }

            return authenticated;
        }
    }
}

所有可执行程序都必须在已编译为exe的项目中的某个位置具有Main函数。

如果只想编译一个类(例如,编译为dll),则必须在Visual Studio中将其设置为“项目类型”。

最简单的方法是创建一个新项目,但是将类库选择为项目类型,然后将代码粘贴到其中。 或者,您可以使用命令行将文件编译为dll,如下所示:

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:library Authenticator.cs

如果您的所有代码仅由上面显示的块组成,那么错误就很明显了。 您的程序中需要Main方法。

按照约定,Main方法是代码开始执行的默认点。 您可以在此处获得更深入的说明

因此,例如,您需要添加以下代码

class Authenticator
{
    static void Main(string[] args)
    {
         Authenticator au = new Authenticator();
         au.InitialValues();
         if(au.Authenticate())
            Console.WriteLine("Authenticated");
         else
            Console.WriteLine("NOT Authenticated");
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();

    }

    // Move the boolen variable inside the method
    public bool Authenticate()
    {
        bool authenticated = false;
        Console.WriteLine("Please enter a username");
        string inputUsername = Console.ReadLine();

        Console.WriteLine("Please enter your password");
        string inputPassword = Console.ReadLine();

        if (dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword)
        {
            authenticated = true;
        }
        else
        {
            authenticated = false;
        }

        return authenticated;
    }
}

顺便说一句,您应该删除输入中传递给Authenticate方法的参数。 您应该将其声明为内部变量,根据检查结果进行设置并将其返回。

但是,您可以完全删除该变量

    ....
    return (dictionary.ContainsKey(inputUsername)) && 
           (dictionary[inputUsername] == inputPassword)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM