C# 函数

PHPz
发布: 2024-09-03 15:13:53
原创
1142 人浏览过

C#函数是C#程序的重要组成部分,它可以由几个元素组成,例如用作函数引用的函数名、函数中操作的数据的返回类型、函数的逻辑体、可以作为函数参数传递的参数,以及用于定义程序内函数的可访问性的访问说明符。可以集成到 C# 程序中的不同函数是带参数或不带参数的函数组合,可以有或没有返回值,具体取决于提供的要求。

函数中有几个组件 –

  • 我们有一个唯一的名称,称为函数名称来进行函数调用。
  • 我们将使用返回类型来指定返回值数据类型。
  • 包含可执行语句的语句块称为主体。
  • 我们可以在函数调用期间将函数作为参数参数列表传递。
  • 要指定应用程序中函数的可访问性,我们可以使用访问说明符。

不同的 C# 函数

  • 没有参数(arguments)和返回类型。
  • 有参数但没有返回类型。
  • 使用参数(参数)和返回类型。
  • 没有参数(arguments),有返回值。

C# 函数语法

<access-specifier><return-type>FunctionName(<parameters>)
{
// function body
// return statement
}
登录后复制

在上述语法中,返回语句、参数和访问说明符是可选的。

 Functional Aspects  Syntax(Function)
With parameters and with
return values
Declaration: int display ( int );

Function call: display ( value );

Function definition:
int display(int value)
{
statements;
return value;
}

With parameters and without
return values
Declaration: void display ( int );

Call: display (value);

Function definition:
void display ( int value)
{
statements;
}

 Without parameters and without
return values
Declaration: void display ();

Call: display ();

Definition:
void display ()
{
statements;
}

Without parameters and with
return values
Declaration: int display ( );

Call: display ( );

Definition:
int display ( )
{
statements;
return value;
}

 功能方面  语法(函数) 带参数并带
返回值 声明:int display ( int ); 函数调用:display ( value ); 函数定义:
int显示(int值)
{
声明;
返回值;
} 带参数和不带参数
返回值 声明:无效显示 ( int ); 调用:显示(值); 函数定义:
void 显示(int 值)
{
声明;
}  不带参数和不带
返回值 声明:void display (); 调用:display (); 定义:
无效显示()
{
声明;
} 不带参数和带
返回值 声明:int display ( ); 调用:显示 ( ); 定义:
int 显示 ( )
{
声明;
返回值;
} 表>

If a function’s return value is “void,” it cannot return any values to the calling function.

Note: If the return value of the function, such as “int, double, float, string, etc.” is other than void, then it can return values to the calling function.

1. Using Without Parameters and Without Return Type

We specified the function with no parameter and no return type, a function that does not return any values here, as void type as a return type value. In this program, any values should not be passed to the function call Display(), and also, there are no values that are returned from this function call to the main function.

Let’s see the example with a function build without a return type and parameter,

Example:

Code:

using System;
namespace FunctionSamples
{
class Program_A
{
// User defined function without return type and parameter
public void Display()
{
Console.WriteLine("Non Parameterized Function"); // No return statement
}
static void Main(string[] args) // Main Program
{
Program_A program = new Program_A (); // to create a new Object
program.Display(); // Call the Function
}
}
}
登录后复制

Output:

C# 函数

2. Using With Parameters (Arguments) and Without Return Type

In this program, a string is passed as a parameter to the function. This function’s return type is “void,” and no values can be returned. The value of the string is manipulated and displayed inside the function itself.

Example:

Code:

using System;
namespace FunctionSample
{
class Program_B
{
public void Display(string value) // User defined function without return type
{
Console.WriteLine("Hello " + value); // No return statement
}
static void Main(string[] args) // Main function
{
Program_B program = new Program_B(); // Creating Objec
program.Display("Welcome to C# Functions"); // Calling Function
}
}
}
登录后复制

Output:

C# 函数

3. Using With Parameters (Arguments) and with Return Type

In this program, a string is passed as a parameter to the function. The return type of this function is “string,” and the return value of the string can be returned from the function. The value of the string is manipulated and displayed inside the function itself.

Example

Code:

using System;
namespace FunctionsSample
{
class Program_C
{
// User defined function
public string Show(string message)
{
Console.WriteLine("Inside the Show Function Call");
return message;
}
// Main function
static void Main(string[] args)
{
Program_C program = new Program_C();
string message = program.Show("C# Functions");
Console.WriteLine("Hello "+message);
}
}
}
登录后复制

Output:

C# 函数

4. Using Without Parameters (Arguments) and with Return Value

In this program, arguments or parameters will not be passed to the function “calculate” but to the main function; the values are returned from this calculate () function call. The variables a and b values are calculated in the function called “calculate,” and in the main function, the sum of these values is returned as a result.

Example:

Code:

using System;
namespace FunctionsSample
{
class Program_D
{
public void calculate()
{
int a = 50, b = 80, sum;
sum = a + b;
Console.WriteLine("Calculating the given to values: " +sum);
}
static void Main(string[] args) // Main function
{
Program_D addition =new Program_D();
addition.calculate();
}
}
}
登录后复制

Output:

C# 函数

C# Passing Parameters to Methods

When creating a method with arguments/parameters in c#, we must pass arguments/parameters to that specified method when calling our application’s function. We have several ways to pass parameters to the method; let’s see the parameters/arguments.

Parameters Description
Value Parameters Value parameters are called the “input parameters.” Instead of the original parameters, the input parameters will pass a copy of the actual value; due to that, there will not be any cause or changes made to the parameter during the called method, and it will not affect on original values while the control passes to the caller function.
Reference Parameters Reference parameters are called the “input/output parameters.” The reference parameter will pass the reference memory of the original parameters. Thus, the changes/alteration made to the parameters in called method, while the control returns to the caller function, affects the actual values.

Output Parameters

It is an “output parameter” like the reference type parameters. The only difference is there is no need to initialize it before passing the data.

Conclusion – C# Functions

In this article, we well-read the usage of the functions/ methods available in C# and learned the different C# functions. I hope this article has helped you understand the several functional aspects of C#.

以上是C# 函数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板