目次
別の C# 関数
1. Using Without Parameters and Without Return Type
2. Using With Parameters (Arguments) and Without Return Type
3. Using With Parameters (Arguments) and with Return Type
4. Using Without Parameters (Arguments) and with Return Value
C# Passing Parameters to Methods
Conclusion – C# Functions

C# 関数

Sep 03, 2024 pm 03:13 PM
c# c# tutorial

C# 関数は、関数の参照として使用される関数名、関数内で操作されるデータの戻り値の型、関数の論理本体、関数の引数として渡すことができるパラメーターと、プログラム内の関数のアクセス可能性を定義するためのアクセス指定子です。 C# プログラムに統合できるさまざまな関数は、パラメータ付きまたはパラメータなしの関数の組み合わせであり、指定された要件に応じて戻り値を持つ場合と持たない場合があります。

関数にはいくつかのコンポーネントがあります –

  • 関数呼び出しを行うための関数名と呼ばれる一意の名前があります。
  • 戻り値のデータ型を指定するには、戻り値の型を使用します。
  • 実行可能なステートメントを含むステートメントのブロックは、ボディと呼ばれます。
  • 関数呼び出し中にパラメータ引数のリストとして関数を渡すことができます。
  • アプリケーション内の関数のアクセシビリティを指定するには、Access 指定子を使用できます。

別の C# 関数

  • パラメータ(引数)と戻り値の型なし。
  • パラメータ(引数)はありますが、戻り値の型はありません。
  • パラメータ(引数)と戻り値の型を使用します。
  • パラメータ(引数)なし、戻り値あり。

C# 関数の構文

<access-specifier><return-type>FunctionName(<parameters>)
{
// function body
// return statement
}
ログイン後にコピー

上記の構文では、Return ステートメント、パラメータ、アクセス指定子はオプションです。

 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値)
{
ステートメント;
戻り値;
} パラメータありとパラメータなし
戻り値 宣言: void display ( int ); 呼び出し: display (値); 関数定義:
void表示(int値)
{
ステートメント;
} パラメータなしとパラメータなし
戻り値 宣言: void display (); 呼び出し: display (); 定義:
void 表示 ()
{
ステートメント;
} パラメータなしとパラメータあり
戻り値 宣言: int display ( ); 呼び出し: 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 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

C# を使用した Active Directory C# を使用した Active Directory Sep 03, 2024 pm 03:33 PM

C# を使用した Active Directory のガイド。ここでは、Active Directory の概要と、C# での動作方法について、構文と例とともに説明します。

C# の乱数ジェネレーター C# の乱数ジェネレーター Sep 03, 2024 pm 03:34 PM

C# の乱数ジェネレーターのガイド。ここでは、乱数ジェネレーターの仕組み、擬似乱数の概念、安全な数値について説明します。

C# データ グリッド ビュー C# データ グリッド ビュー Sep 03, 2024 pm 03:32 PM

C# データ グリッド ビューのガイド。ここでは、SQL データベースまたは Excel ファイルからデータ グリッド ビューをロードおよびエクスポートする方法の例について説明します。

C# の階乗 C# の階乗 Sep 03, 2024 pm 03:34 PM

C# の Factorial のガイド。ここでは、C# での階乗の概要について、さまざまな例とコード実装とともに説明します。

マルチスレッドと非同期C#の違い マルチスレッドと非同期C#の違い Apr 03, 2025 pm 02:57 PM

マルチスレッドと非同期の違いは、マルチスレッドが複数のスレッドを同時に実行し、現在のスレッドをブロックせずに非同期に操作を実行することです。マルチスレッドは計算集約型タスクに使用されますが、非同期はユーザーインタラクションに使用されます。マルチスレッドの利点は、コンピューティングのパフォーマンスを改善することですが、非同期の利点はUIスレッドをブロックしないことです。マルチスレッドまたは非同期を選択することは、タスクの性質に依存します。計算集約型タスクマルチスレッド、外部リソースと相互作用し、UIの応答性を非同期に使用する必要があるタスクを使用します。

C# のパターン C# のパターン Sep 03, 2024 pm 03:33 PM

C# のパターンのガイド。ここでは、C# のパターンの概要と上位 3 種類について、その例とコード実装とともに説明します。

C# の素数 C# の素数 Sep 03, 2024 pm 03:35 PM

C# の素数ガイド。ここでは、C# における素数の導入と例を、コードの実装とともに説明します。

XMLの形式を変更する方法 XMLの形式を変更する方法 Apr 03, 2025 am 08:42 AM

XML形式を変更する方法はいくつかあります。Atepadなどのテキストエディターを使用して手動で編集する。 XmlBeautifierなどのオンラインまたはデスクトップXMLフォーマットツールを使用して自動的にフォーマットします。 XSLTなどのXML変換ツールを使用して変換ルールを定義します。または、Pythonなどのプログラミング言語を使用して解析および操作します。元のファイルを変更してバックアップするときは注意してください。

See all articles