C#:匿名方法与命名方法 [英] C#: Anonymous method vs Named method

查看:26
本文介绍了C#:匿名方法与命名方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SO 新手,每天都在使用技术 (C#) 行话进行编程和学习.

I'm new to SO and programming and learning day by day with bits and pieces of tech (C#) jargons.

谷歌搜索了一段时间后,下面是我对methods

After Googling for a while, below is what I've researched about methods

  1. 方法是一个语句块,用于代码的可重用性&它还支持使用不同的 SIGNATURE 进行重载....例如:drawShape(2pts), drawShape(3pts) 等等...

  1. A Method is a block of statements, which serves for code reusability & it also supports overloading with different SIGNATURE....for ex: drawShape(2pts), drawShape(3pts) etc...

Anonymous 方法是一种带有语句块的方法,但没有名称....(因为现在问还为时过早,在wt情况下我们遇到匿名方法……任何文章、样本……)

An Anonymous method is one with block of statements, but no name....(as its premature to ask, in wt situation we come across anonymous method...any articles, samples ...)

命名方法:这是一个链接,但是最后我没有明白命名方法实际上是什么......

Named method: Here's a link but at the end i didn't get what Named Method actually is...

谁能解释什么是命名"方法,以及我们在哪里使用匿名方法?

Can anyone explain what a "Named" method is, and where do we use anonymous method?

推荐答案

命名方法是一种您可以通过其名称调用的方法(例如,它是一个具有名称的函数).例如,您定义了一个将两个数字相加的函数:

A named method is a method you can call by its name (e.g. it is a function that has a name). For example, you have defined a function to add two numbers:

int f(int x, int y)
{
    return x+y;
}

您可以像这样按名称调用此方法:f(1, 2);.

You would call this method by its name like so: f(1, 2);.

匿名方法是一种作为参数传递给函数而无需其名称的方法.这些方法可以在运行时构建,也可以在编译时从 lambda 表达式求值.

Anonymous method is a method that is passed as an argument to a function without the need for its name. These methods can be constructed at runtime or evaluated from a lambda expression at compile time.

这些方法经常用于LINQ查询,例如:

These methods are often used in LINQ queries, for example:

int maxSmallerThan10 = array.Where(x => x < 10).Max();

表达式 x =>×<10 被称为 lambda 表达式,其结果是一个匿名函数,该函数将由 Where 方法运行.

The expression x => x < 10 is called a lambda expression and its result is an anonymous function that will be run by the method Where.

如果你是初学者,我建议你先阅读一些更基础的东西.查看以下链接:

If you are a beginner, I would suggest you first read about more basic stuff. Check out the following links:

这篇关于C#:匿名方法与命名方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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