Share yield return vs return c#

nguyenvy6464

New member
#năng suất-Return-Vs-Return-C#
#c#
#Programming
#tutorial
#development ## Sự khác biệt giữa lợi nhuận và lợi nhuận trong C#là gì?

Trong C#, `Yield Return` và` Return` đều được sử dụng để thoát một hàm sớm, nhưng chúng có ý nghĩa và cách sử dụng khác nhau.

* `Return Return` được sử dụng để đình chỉ việc thực hiện hàm và trả về giá trị cho người gọi.Hàm có thể được nối lại sau, bằng cách gọi một lần nữa, return return` hoặc bằng cách gọi phương thức `next
* `return` được sử dụng để chấm dứt thực thi hàm và trả về giá trị cho người gọi.Hàm không thể được nối lại sau khi nó đã bị chấm dứt với `return`.

Dưới đây là một ví dụ đơn giản minh họa sự khác biệt giữa 'lợi nhuận năng suất' và `return`:

`` `C#
Công cộng tĩnh điện
{
int a = 0;
int b = 1;

while (đúng)
{
năng suất trả lại a;

a = b;
b = a + b;
}
}

// Mã này in chuỗi Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, ...
for (int i = 0; i <10; i ++)
{
Console.WriteLine (fibonacci (). Elementat (i));
}
`` `

Trong ví dụ này, hàm `fibonacci ()` sử dụng `retur return` để trả về mỗi số fibonacci khi được tính toán.Điều này cho phép hàm được nối lại nhiều lần, để nó có thể tính toán số Fibonacci tiếp theo mỗi khi nó được gọi.

Ngược lại, câu lệnh `return` trong hàm` main () `chấm dứt việc thực hiện chương trình và trả về giá trị` 0` cho hệ điều hành.Điều này có nghĩa là hàm `fibonacci ()` sẽ không được gọi lại và chương trình sẽ kết thúc.

## Khi nào bạn nên sử dụng lợi nhuận lợi nhuận so với trả lại?

Bạn nên sử dụng `mel return` khi bạn muốn tạm dừng việc thực thi hàm và trả về giá trị cho người gọi.Điều này rất hữu ích khi bạn đang viết một chức năng cần lặp lại một bộ sưu tập các mục và bạn muốn trả lại các mục cùng một lúc.

Bạn nên sử dụng `return` khi bạn muốn chấm dứt thực thi hàm và trả lại giá trị cho người gọi.Điều này rất hữu ích khi bạn đang viết một hàm không cần lặp lại một bộ sưu tập các mục và bạn chỉ muốn trả về một giá trị duy nhất.

## Phần kết luận

`Yield return` và` return` đều được sử dụng để thoát ra một hàm sớm, nhưng chúng có ý nghĩa và sử dụng khác nhau.`Self Return` được sử dụng để đình chỉ thực thi hàm và trả về giá trị cho người gọi, trong khi` return` được sử dụng để chấm dứt việc thực thi hàm và trả về giá trị cho người gọi.

Bạn nên sử dụng `mel return` khi bạn muốn tạm dừng việc thực thi hàm và trả về giá trị cho người gọi.Bạn nên sử dụng `return` khi bạn muốn chấm dứt thực thi hàm và trả lại giá trị cho người gọi.
=======================================
#yield-return-vs-return-c#
#c#
#Programming
#tutorial
#development ## What is the difference between yield return and return in C#?

In C#, `yield return` and `return` are both used to exit a function early, but they have different meanings and uses.

* `yield return` is used to suspend the execution of a function and return a value to the caller. The function can be resumed later, either by calling `yield return` again or by calling the `next()` method on the `IEnumerable` object that is returned by `yield return`.
* `return` is used to terminate the execution of a function and return a value to the caller. The function cannot be resumed after it has been terminated with `return`.

Here is a simple example that illustrates the difference between `yield return` and `return`:

```c#
public static IEnumerable<int> Fibonacci()
{
int a = 0;
int b = 1;

while (true)
{
yield return a;

a = b;
b = a + b;
}
}

// This code prints the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, ...
for (int i = 0; i < 10; i++)
{
Console.WriteLine(Fibonacci().ElementAt(i));
}
```

In this example, the `Fibonacci()` function uses `yield return` to return each Fibonacci number as it is calculated. This allows the function to be resumed multiple times, so that it can calculate the next Fibonacci number each time it is called.

In contrast, the `return` statement in the `main()` function terminates the execution of the program and returns the value `0` to the operating system. This means that the `Fibonacci()` function will not be called again, and the program will end.

## When should you use yield return vs return?

You should use `yield return` when you want to suspend the execution of a function and return a value to the caller. This is useful when you are writing a function that needs to iterate over a collection of items, and you want to return the items one at a time.

You should use `return` when you want to terminate the execution of a function and return a value to the caller. This is useful when you are writing a function that does not need to iterate over a collection of items, and you just want to return a single value.

## Conclusion

`yield return` and `return` are both used to exit a function early, but they have different meanings and uses. `yield return` is used to suspend the execution of a function and return a value to the caller, while `return` is used to terminate the execution of a function and return a value to the caller.

You should use `yield return` when you want to suspend the execution of a function and return a value to the caller. You should use `return` when you want to terminate the execution of a function and return a value to the caller.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top