C# 函數

PHPz
發布: 2024-09-03 15:13:53
原創
1176 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板