有许多错误的 C# 应用程序

Posted

技术标签:

【中文标题】有许多错误的 C# 应用程序【英文标题】:C# Application with Numerous Errors 【发布时间】:2012-10-25 11:09:22 【问题描述】:

在我正在编写的 C# 应用程序中遇到一些错误,我对此感到很开心。 我不断收到的错误是:

加密和解密调用必须有返回类型 Console.WriteLine 被用作方法 static void encrypt(string[] args) 期望的类、委托、接口或结构
namespace ConsoleApplication1

    class Program
    
        static void Main(string[] args)
        
            string pw ="", hash =""; //Declare an intialise variables

            if (args.Length < 4) // Test to see if correct number of arguments have been passed
            
                Console.WriteLine("Please use command line arguments in this format: encrypt -e (or -d) password-to-encrypt-with input-file output-file");
                Environment.Exit(0);
            

            if (args[1].Length < 10 || args[1].Length > 40) // Test to see if the password is between 10 and 40 characters
            
                Console.WriteLine("Please use a password between 10 and 40 characters");
                Environment.Exit(0);
            

            switch (args[0]) //Uses first argument value to drive switch statement (-e or -d)
            
                case "-e":
                encrypt(string[] args);
                break;

                case "-d":
                decrypt(string[] args);
                break;

                default:
                Console.WriteLine("When using the program please use -e to encrypt and -d to decrypt");
                break;
                    
         //End of MAIN

        static void encrypt(string[] args)  //Function to encrypt
        
            string inputtext =""; //Initialise Varible (Ensure it is empty)
            inputtext=System.IO.File.ReadAllText(args[2]); //Read file in an assign to input text
            return;
        

        static void decrypt(string[] args)  //Function to decrypt
        
            string inputtext =""; //Initialise Varible (Ensure it is empty)
            inputtext=System.IO.File.ReadAllText(args[2]); //Read file in an assign to input text
            return;
        
      

任何帮助将不胜感激! 阿利斯泰尔

【问题讨论】:

你不需要初始化变量,就在你给它们另一个值之前。 【参考方案1】:

调用方法时,不能指定参数的类型。所以:

        case "-e":
        encrypt(args);
        break;

【讨论】:

【参考方案2】:

除了 Hans 所说的,您还提到了有关方法中返回类型的错误。

您的 encryptdecrypt 方法具有 return 语句,但它们是 void 方法,这意味着它们没有任何返回类型。

要么给它一个你想要返回的类型(大概是你正在操作的字符串),要么完全删除return 语句。您不需要显式地将return 放在方法的末尾以使其退出该方法。无论如何它都会这样做。

两个小的专业提示,我会在不同的行上声明你的字段,而不是全部捆绑在一起(使用你声明 pwhash 的方式)并为 System.IO 添加一个 using 指令,所以你不用打System.IO.File.ReadAllText,直接打File.ReadAllText即可。

【讨论】:

以上是关于有许多错误的 C# 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

c#用许多“x”替换字符串[重复]

是否可以将带有许多包的 Python 脚本嵌入到 C# 中?

C# WPF 如何在向 Stackpanel 添加许多 UI 元素时保持应用程序响应

Mongodb 如何使用 C# 驱动程序更新许多文档

关于Oracle Spatial的数据格式 C#

对象计算验证 C#