Share yield c#

cuongquyet815

New member
** Năng suất trong C#**

Năng suất là một từ khóa trong ngôn ngữ lập trình C# cho phép một hàm trả về một giá trị và sau đó đình chỉ thực thi cho đến lần tiếp theo nó được gọi.Điều này có thể được sử dụng để thực hiện các trình lặp, máy phát và các mẫu lập trình không đồng bộ khác.

## Năng suất là gì?

Khi một hàm sử dụng từ khóa năng suất, nó sẽ trả về một giá trị và sau đó đình chỉ thực thi.Hàm sau đó có thể được nối lại sau một thời gian sau, khi cần giá trị tiếp theo.Điều này cho phép chức năng xử lý dữ liệu theo kiểu phát trực tuyến, mà không phải lưu trữ tất cả dữ liệu trong bộ nhớ cùng một lúc.

## Cách sử dụng năng suất

Để sử dụng năng suất, bạn chỉ cần thêm từ khóa năng suất vào câu lệnh trả về chức năng của bạn.Ví dụ:

`` `C#
công khai ienumerere <tt> getnumbers ()
{
for (int i = 0; i <10; i ++)
{
lợi nhuận trở lại i;
}
}
`` `

Hàm này sẽ trả về các số 0 đến 9, mỗi lần một.Khi hàm được gọi, giá trị đầu tiên (0) sẽ được trả về và hàm sẽ đình chỉ thực thi.Lần tiếp theo hàm được gọi, giá trị 1 sẽ được trả về, v.v.

## Iterators and Generators

Năng suất có thể được sử dụng để thực hiện các trình lặp và máy phát điện.Một trình lặp là một lớp có thể được sử dụng để lặp lại một bộ sưu tập dữ liệu.Trình tạo là một hàm có thể được sử dụng để tạo một iterator.

Để tạo một trình lặp, bạn chỉ cần tạo một lớp thực hiện giao diện ienumerable <t>.Giao diện <T> ienumerere xác định một phương thức duy nhất, được gọi là getenumerator (), trả về một đối tượng ienumerator <t>.Đối tượng ienumerator <t> có thể được sử dụng để lặp lại trong bộ sưu tập dữ liệu.

Để tạo một trình tạo, bạn chỉ cần tạo một chức năng sử dụng từ khóa năng suất.Hàm sẽ trả về một đối tượng <t> có thể sử dụng được để lặp lại dữ liệu.

## Lập trình không đồng bộ

Năng suất cũng có thể được sử dụng để thực hiện các mẫu lập trình không đồng bộ.Lập trình không đồng bộ cho phép bạn thực hiện các tác vụ mà không chặn luồng thực thi chính.Điều này có thể hữu ích cho các tác vụ mất nhiều thời gian để hoàn thành, chẳng hạn như đọc từ tệp hoặc kết nối với cơ sở dữ liệu.

Để sử dụng năng suất cho lập trình không đồng bộ, bạn chỉ cần sử dụng các từ khóa Async và Await.Từ khóa Async nói với trình biên dịch rằng hàm không đồng bộ và từ khóa đang chờ đợi trình biên dịch chờ đợi hoạt động không đồng bộ hoàn thành.

## ví dụ

Dưới đây là một số ví dụ về cách sử dụng năng suất:

* Để triển khai trình lặp trả về các số 0 đến 9:

`` `C#
Số lượng lớp công khai:
{
Công khai
{
for (int i = 0; i <10; i ++)
{
lợi nhuận trở lại i;
}
}

Ienumerator ienumerere.getenumerator ()
{
trả lại getenumerator ();
}
}
`` `

* Để tạo một trình tạo trả về chuỗi Fibonacci:

`` `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;

int c = a + b;
a = b;
B = C;
}
}
`` `

* Để thực hiện chức năng không đồng bộ đọc từ một tệp:

`` `C#
Công khai Async Task <String> readFileAnync (tên tệp chuỗi)
{
sử dụng (fileStream filestream = new fileStream (fileName, filemode.open, fileaccess.read)))
{
byte [] bộ đệm = byte mới [1024];

int bytesread;
while ((bytesread = Await fileStream.ReadAsync (bộ đệm, 0, buffer.length))> 0)
{
// Làm điều gì đó với dữ liệu trong bộ đệm
}
=======================================
**Yield in C#**

Yield is a keyword in the C# programming language that allows a function to return a value and then suspend execution until the next time it is called. This can be used to implement iterators, generators, and other asynchronous programming patterns.

## What is yield?

When a function uses the yield keyword, it returns a value and then suspends execution. The function can then be resumed at a later time, when the next value is needed. This allows the function to process data in a streaming fashion, without having to store all of the data in memory at once.

## How to use yield

To use yield, you simply need to add the yield keyword to the return statement of your function. For example:

```c#
public IEnumerable<int> GetNumbers()
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}
```

This function will return the numbers 0 to 9, one at a time. When the function is called, the first value (0) will be returned, and the function will suspend execution. The next time the function is called, the value 1 will be returned, and so on.

## Iterators and generators

Yield can be used to implement iterators and generators. An iterator is a class that can be used to iterate over a collection of data. A generator is a function that can be used to create an iterator.

To create an iterator, you simply need to create a class that implements the IEnumerable<T> interface. The IEnumerable<T> interface defines a single method, called GetEnumerator(), which returns an IEnumerator<T> object. The IEnumerator<T> object can be used to iterate over the collection of data.

To create a generator, you simply need to create a function that uses the yield keyword. The function will return an IEnumerable<T> object, which can be used to iterate over the data.

## Asynchronous programming

Yield can also be used to implement asynchronous programming patterns. Asynchronous programming allows you to perform tasks without blocking the main thread of execution. This can be useful for tasks that take a long time to complete, such as reading from a file or connecting to a database.

To use yield for asynchronous programming, you simply need to use the async and await keywords. The async keyword tells the compiler that the function is asynchronous, and the await keyword tells the compiler to wait for the asynchronous operation to complete.

## Examples

Here are some examples of how yield can be used:

* To implement an iterator that returns the numbers 0 to 9:

```c#
public class NumberIterator : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
{
for (int i = 0; i < 10; i++)
{
yield return i;
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
```

* To create a generator that returns the Fibonacci sequence:

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

while (true)
{
yield return a;

int c = a + b;
a = b;
b = c;
}
}
```

* To implement an asynchronous function that reads from a file:

```c#
public async Task<string> ReadFileAsync(string filename)
{
using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024];

int bytesRead;
while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
// Do something with the data in the buffer
}
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top