简体   繁体   English

C#中的语法混淆

[英]Syntax confusion in C#

So I'm looking at a project that calls a function I've never seen before, and I'm only beginning my familiarity with C# syntax. 因此,我正在看一个项目,该项目调用一个我从未见过的函数,而我才刚刚开始熟悉C#语法。 I've worked with Python and Javascript for some weeks now, and I'm closer to experienced with them, but this syntax still baffles me. 我已经使用Python和Javascript已有数周了,而我对它们的使用更近了,但是这种语法仍然使我感到困惑。

private static void GENERIC_FUNCTION_NAME(string[] args)
    {
        GENERIC_OBJECT_NAME.OTHER_GENERIC_FUNCTION_NAME();

        string path = args == null || args.Length == 0 ?
            @"C:\GENERIC\SYSTEM\PATH" :
            args[1];

        var GENERIC_VARIABLE_NAME = new GENERIC_OBJECT_NAME();
        GENERIC_VARIABLE_NAME.Open(path);
    }

I'm confused by the syntax of the parameters of this function. 我对该函数参数的语法感到困惑。

You see, this function was written in a C# console application and has lots of C++ components in the project that do the work it's meant to trigger. 您会看到,此函数是用C#控制台应用程序编写的,并且在项目中具有许多C ++组件,可以完成要触发的工作。 But I've been told to build a GUI for it that achieves the same goal here. 但是有人告诉我为此构建一个GUI,以实现此处的相同目标。 I'm not even sure what gets passed as args ... Because I don't see it anywhere in the script next to this code, which is the only function inside the Program.cs that was written when I got the files to work with. 我什至不知道是什么作为args传递的...因为在此代码旁边的脚本中的任何地方都看不到它,这是当我使文件工作时在Program.cs中编写的唯一函数用。

Because of this, I can't make a button connect to this function. 因此,我无法使按钮连接到该功能。 I don't know how to integrate object sender, EventArgs e so that the button can be connected (and if I do it directly, ie, " (object sender, EventArgs e, string[] args) ", I get an error. What am I doing wrong? And how might I integrate the function and make it button addressable? 我不知道如何集成object sender, EventArgs e以便可以连接按钮(如果直接进行操作,即“ (object sender, EventArgs e, string[] args) ”),则会收到错误消息。我在做什么错?我该如何整合功能并使按钮可寻址?

EDIT: I've moved the questions regarding the ternary operator here for future readers' sanity. 编辑:为了将来读者的理智,我将有关三元运算符的问题移到了这里 If you would like, please repost your input on this, as you guys have already helped me here on it. 如果您愿意,请重新发表您的意见,因为你们已经在这里帮助了我。

i can help with the syntax issue. 我可以帮助解决语法问题。

string path is equal to either @"C:\\GENERIC\\SYSTEM\\PATH" OR (:) args[1] 字符串路径等于@“ C:\\ GENERIC \\ SYSTEM \\ PATH”或(:) args [1]

to determine which one if args == null OR args.length == 0 then go with @"C:\\GENERIC\\SYSTEM\\PATH" if not then go with args[1] it is the same as the following 确定如果args == null或args.length == 0,则使用@“ C:\\ GENERIC \\ SYSTEM \\ PATH”,否则使用args [1],它与以下内容相同

if(args == null || args.Length == 0)
{
    string path = @"C:\GENERIC\SYSTEM\PATH" ;
}
else{
    string path = args[1];
}

Here is some further reading http://msdn.microsoft.com/en-us/library/ty67wk28.aspx 这是一些进一步的阅读http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

I assume your method was called Main , and is by default the only method in Program.cs 我假设您的方法称为Main ,并且默认情况下是Program.cs的唯一方法

This main method takes arguments when the program is executed, such as running through CMD with optional arguments such as app.exe -myArg , and is translated into a string array. 在执行程序时(例如通过CMD运行带有可选参数,例如app.exe -myArg方法),此main方法采用参数,并将其转换为字符串数组。 See more on command line parameters . 有关命令行参数的更多信息,请参见。

If not, you can right click the method name, and View All References , or Find Call hierarchy to figure out what might be calling it. 如果不是,则可以右键单击方法名称,然后单击“ 查看所有引用 ”或“ 查找调用”层次结构以找出可能正在调用的方法。

For your second issue... What you are seeing is a special conditional operator, the ternary operator . 对于第二个问题,您将看到一个特殊的条件运算符,即三元运算符 (And here is a nice tutorial) 是一个不错的教程)

It is used like so: 它的用法如下:

condition ? first_expression : second_expression;

Basically if the statement is true, the first expression is executed, if not, the second is. 基本上,如果该语句为true,则执行第一个表达式,否则执行第二个表达式。 Generally speaking it is a small shortcut for if/else blocks, and should be used for only small statements. 一般来说,它是if/else块的小捷径,仅应用于小语句。 Nesting the ternary operator is largely frowned upon. 嵌套三元运算符在很大程度上是不赞成的。

So if args == null || args.Length == 0 因此,如果args == null || args.Length == 0 args == null || args.Length == 0 Then path = @"C:\\GENERIC\\SYSTEM\\PATH" , if not, it equals args[1] args == null || args.Length == 0然后path = @"C:\\GENERIC\\SYSTEM\\PATH" ,如果不是,则等于args[1]

It is equivalent to your standard if block 它相当于您的标准, if

string path;
if(args == null || args.Length == 0)
{
   path = @"C:\GENERIC\SYSTEM\PATH";
}
else
{
   path = args[1];
}

Now that that is taken care of, if you double click your button in the designer, it will take you (or generate) a method that is executed when the button is clicked. 现在已经解决了,如果您在设计器中双击按钮,它将带您(或生成)单击按钮时执行的方法。 From there you can call this method with it's arguments (Which if it is main , I'm not sure why you would be doing this. But I'm not sure if I quite understand the issue) 从那里可以用它的参数调用此方法(如果它是main ,我不确定为什么要这样做。但是我不确定我是否很理解这个问题)

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

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