C#中函数和函数()之间的区别是什么?

时间:2017-04-02 09:43:52

标签: c#

例如,     Thread t = new Thread(function); 为什么不     Thread t = new Thread(function()); 谢谢。

5 个答案:

答案 0 :(得分:4)

  • function()指的是您可以调用的函数的地址
  • function获得回报 功能的价值
  • 第三种方式可能是function() 创建一个新的匿名函数,它调用() => function()(在这种情况下是一个简单的包装器)

由于function不返回其他功能

function()

不起作用,因为线程需要一个函数来调用。这意味着您必须使用

Thread t = new Thread(function()); 

Thread t = new Thread(function); 

将函数传递给线程。

答案 1 :(得分:1)

<div id="riDiv" class="rightDiv" runat="server"> <asp:ListView runat="server" ID="lvItemDetails" style="border:solid; border-color:blue; border-width:10px;"> <LayoutTemplate> <div> <table> <tr runat="server" id="itemplaceholder" class="TopboxDiv"> </tr> <tr runat="server" id="itemPlaceHolder2"> </tr> </table> </div> </LayoutTemplate> <ItemTemplate> <tr> <div class="TopboxDiv"> <asp:Literal ID="litItemDesc" Text='<%# ".." + Eval("item_description") %>' runat="server" /> </div> </tr> <tr> <div class="MidboxDivWrapper" runat="server"> <div class="leftTagDiv"> <div class="pc">Price</div> </div> <div id="middiv" class="MidboxDiv" runat="server"> <%--<div id="innerRadDiv">--%> <asp:Panel runat="server" ID="pnlMidBoxDiv" BorderStyle="Solid" BorderColor="Pink" BackColor="Aqua" style="z-index:2 !important;"> <span> <asp:RadioButtonList ID="rdCrService" runat="server" DataTextField="ServiceName" DataValueField="ServiceName" RepeatDirection="Horizontal" CssClass="radioDiv" > </asp:RadioButtonList> </span> </asp:Panel> <%--</div>--%> <div class="radioDiv"> <asp:RadioButton id="rdPost" Text="Postoffice Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup" /> <asp:Literal ID="litPost" Text="Rs. 100" runat="server" /> </div> <%--<div class="radioDiv"> <asp:RadioButton id="rdOCS" Text="OCS Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup"/> <asp:Literal ID="litOCS" Text="Rs. 110" runat="server" /> </div> <div class="radioDiv"> <asp:RadioButton id="rdTCS" Text="TCS Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup"/> <asp:Literal ID="litTCS" Text="Rs. 120" runat="server" /> </div> <div class="radioDiv"> <asp:RadioButton id="rdOther" Text="Other Price" runat="server" CssClass="radiotext" GroupName="radPriceGroup"/> <asp:Literal ID="litOther" Text="Rs. 120" runat="server" /> </div>--%> </div> </div> </tr> <tr> <div class="bottomDivItemWrapper"> <div class="leftTagDiv"> <div class="pc">Color</div> </div> <div class="bottomDivItem"> <div class="radioDiv"> <%--<asp:RadioButton ID="Rad1" Text='<%# Eval("ServiceName") %>' runat="server" GroupName="testGrp" CssClass="radiotext"/>--%> <asp:RadioButton id="rdWhite" Text="white" runat="server" CssClass="radiotext" /> </div> </div> </div> <div class="OrderDivWrapper"> add cart and wishlist buttons. write cart and wishlist code functions so it can be called again and again. </div> </tr> </ItemTemplate> </asp:ListView> </div> 是函数(签名)的委托,不会被执行。 function将被执行,你将得到函数的结果。

答案 2 :(得分:1)

这一行

Thread t = new Thread(function);

是此行的简短版本:

Thread t = new Thread(new ThreadStart(function));

它使用C#2中引入的方法组语法从方法名称构造委托ThreadStart

另一方面,这一行

Thread t = new Thread(function());

使用function方法返回的委托创建一个线程。当然function必须返回ThreadStart委托才能进行上述编译。

答案 3 :(得分:1)

在我回答之前,我有一个非常重要的问题,为什么在当前版本的.Net中使用Thread,使用Task和Parallel API,它们更清晰,更抽象。要了解其罚款,但不再使用

以下是Thread类的源代码,请仅使用ThreadStart委托检查构造函数,粘贴在下面:

 [System.Security.SecuritySafeCritical]  // auto-generated
        public Thread(ThreadStart start) {
            if (start == null) {
                throw new ArgumentNullException("start");
            }
            Contract.EndContractBlock();
            SetStartHelper((Delegate)start,0);  //0 will setup Thread with default stackSize
        }

以下是ThreadStart委托

的源代码
[System.Runtime.InteropServices.ComVisible(true)]
    public delegate void ThreadStart();

所以Thread类只需要一个Delegate,返回void而没有输入,这是Thread t = new Thread(function)中的函数指向的,实际上这是一个简短的形式,称为方法分组,否则完全成熟的形式将创建一个ThreadStart对象,为其分配一个映射到其签名的函数。以下是其他选项:

  1. () => Function() // Lambda语法,直接在Thread构造函数中使用
  2. ThreadStart ts = new ThreadStart(Function) //变量ts可以在Thread构造函数中使用

答案 4 :(得分:0)

功能只是&#34;功能&#34;变量

function()对它进行求值,并将其返回值作为参数传递给线程构造函数

相关问题