如何创建功能 [英] How do I create a function

查看:120
本文介绍了如何创建功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多代码需要重复使用,我想创建这样的东西





功能(创建1)

//大块代码





然后当我需要调用它时例如:



private void button1_Click(对象发件人,EventArgs e)

{

如果(发生的事情) {



功能(创建1)



}

}



我尝试了什么:



字面上无法在互联网上找到任何内容,但我确实知道过去怎么做:D

解决方案

您可能没有找到它,因为它在C#中被称为'方法',这里是教程: C#方法 [ ^ ]


sy几种编程语言的ntax区分'函数...返回值的方法...'和'不返回值的方法。



C#没有区别除非通过所需的返回类型指示符:要么是一个特殊术语'对于一个不返回任何内容的方法的空;或者,对于返回某个东西的函数,返回的Type的名称。



public void NoReturnMethod()

public int ReturnIntValueMethod (int a,int b)



您可以在方法变量参数声明中使用'out和'ref说明符来启用方法来持久更改或创建for,在方法外定义的变量...该方法是否返回变量。

  public   double  a =  3 ; 
public double b = 4 ;

public double UsesRefMethod( ref double c, ref double d)
{
c = Math.Pow(c, 2 );
d = Math.Pow(d, 2 );

return Math.Sqrt(c + d);
}

// 使用:

double result = UsesOutMethod( ref a, ref b); '

a将是#9,'
b将是#16,' 结果将是#5


I have a big chunk of code that I need to reuse a lot, I want to create something like this


Function(Create1)
// Big chunk of code


and then when i need to call it for example:

private void button1_Click(object sender, EventArgs e)
{
If (something happens) {

Function(Create1)

}
}

What I have tried:

literally can't find anything on the internet, but i did know how to do it in the past :D

解决方案

You probably did not find it because it is called a 'Method' in C#, here is a tutorial: C# Methods[^]


The syntax of several programming languages distinguishes between 'functions ... methods that return a value ... and 'methods that do not return a value.

C# makes no distinction except through the required return-type signifier: either the special term 'void for a method that returns nothing; or, for a function that returns something, the name of the Type that is returned.

public void NoReturnMethod()
public int ReturnIntValueMethod(int a, int b)

You can use the 'out and 'ref specifiers in method variable parameter declarations to enable a method to persist changes, or create for, a variable defined outside a method ... whether that method returns a variable, or not.

public double a = 3;
public double b = 4;

public double UsesRefMethod(ref double c, ref double d)
{
    c = Math.Pow(c, 2);
    d = Math.Pow(d, 2);

    return Math.Sqrt(c + d);
}

// use :  

double result = UsesOutMethod(ref a, ref b);

after this: 'a will be #9, 'b will be #16, 'result will be #5


这篇关于如何创建功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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