C#语句我在函数中不理解 [英] C# statments I do not understand in a Function

查看:70
本文介绍了C#语句我在函数中不理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的c#


我有以下代码可以理解



< pre class ="prettyprint"> public bool SetGroupOn(string Name)
{
if(string.IsNullOrEmpty(Name))
return RemoveGrouping();
返回CheckSource()。SetGroupOn(Name);
}

GroupingSource CheckSource()
{
if(myDGV == null)
抛出新的异常("No target datagridview set");

if(!GridUsesGroupSource)
{
myBS.DataSource = myDGV.DataSource;
myBS.DataMember = myDGV.DataMember;
myDGV.DataSource = myBS;
}
返回myBS;
}

问题是:以下是什么意思?


返回CheckSource()。SetGroupOn(Name); 

什么是RETURN返回?



还有一件事

 public bool SetGroupOn(DataGridViewColumn col )
{
返回SetGroupOn(col == null?null:col.DataPropertyName);
}


在这种情况下RETURN返回什么?



感谢您的帮助

解决方案

第一次返回是调用CheckSource
方法
。它返回一些具有SetGroupOn方法的对象。将针对CheckSource返回的对象调用该方法。返回该调用的结果。你可以将它看作是这样发生的。

 var tempObject = CheckSource(); 
var tempObject2 = tempObject.SetGroupOn(Name);

返回tempObject2;

要确定CheckSource正在做什么以及它返回什么,您可以右键单击该调用并转到"定义"或"转到实现"。您可以对SetGroupOn执行相同操作。 


第二个示例相同。它调用SetGroupOn方法。它使用

条件运算符
确定传递给方法的内容。条件运算符的形式为E?答:B.E被评估。如果E为真,则表达式A是整个表达式的值。如果E为假,则B为该值。对于if语句,它等价
(尽管是表达格式)。

 E?答:B 

//相当于
T temp;
if(E)
temp = A;
else
temp = B;

因此,如果参数col为null,则它将null传递给方法调用。如果不是,那么它将传递列的DataPropertyName属性。


请注意,在两个示例中,正在调用的SetGroupOn方法不是您发布的方法。您提出的方法接受DataGridViewColumn作为参数。我怀疑你的第一个例子中Name是string类型。在第二个例子中,它绝对是
一个字符串。因此,此处使用功能重载。 SetGroupOn的覆盖接受字符串作为参数。要获得实际调用的版本,再次,
右键单击方法调用并使用Go to Definition或Go to Implementation。



I'm new on c#

I have the following code to understand

 public bool SetGroupOn(string Name)
        {
            if (string.IsNullOrEmpty(Name))
                return RemoveGrouping();
            return CheckSource().SetGroupOn(Name);
        }

 GroupingSource CheckSource()
        {
            if (myDGV == null)
                throw new Exception("No target datagridview set");

            if (!GridUsesGroupSource)
            {
                myBS.DataSource = myDGV.DataSource;
                myBS.DataMember = myDGV.DataMember;
                myDGV.DataSource = myBS;
            }
            return myBS;
        }

The question is : What doews the following statment mean ?

return CheckSource().SetGroupOn(Name);

What does the RETURN return ?

One more thing

 public bool SetGroupOn(DataGridViewColumn col)
        {
            return SetGroupOn(col == null ? null : col.DataPropertyName);
        }

What does the RETURN return in this case ?

Thanks for help

解决方案

The first return is calling the CheckSource method. It returns some object that has a SetGroupOn method. That method will be called against the object returned from CheckSource. The results of that call is returned. You can visualize it happening like this.

var tempObject = CheckSource();
var tempObject2 = tempObject.SetGroupOn(Name);

return tempObject2;

To determine what CheckSource is doing and what it returns you can right click the call and Go To Definition or Go To Implementation. You can do the same for SetGroupOn. 

Same for your second example. It is calling the SetGroupOn method. It is using the conditional operator to determine what to pass to the method. The conditional operator is of the form E ? A : B. E is evaluated. If E is true then the expression A is the value of the entire expression. If E is false then B is the value. It is equivalent (albeit in expression format) to an if statement.

E ? A : B

//Equivalent to
T temp;
if (E)
   temp = A;
else
   temp = B;   

So if the parameter col is null then it passes null to the method call. If it isn't then it passes the DataPropertyName property of the column.

Note that in both examples the SetGroupOn method that is being called isn't the one you posted. The method you posed accepts a DataGridViewColumn as an argument. I suspect in your first example that Name is of type string. In the second example it is definitely a string. So function overloading is being used here. There is an override of SetGroupOn that accepts a string as a parameter. To get to the version that is actually being called, again, right click the method call and use Go to Definition or Go to Implementation.


这篇关于C#语句我在函数中不理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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