> For the complete documentation index, see [llms.txt](https://docs.dot-net.uz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dot-net.uz/c-.net/basic/yuqori-daraja/stream-i-o/memorystream.md).

# MemoryStream

Xo'sh do'stlar mana o'tgan darslarda turli xildagi fayllarga ma'lumotlarni yozishni, o'qishni va saqlashni ko'rdingiz, keling endi bizdagi ma'lumotlarni fayllarga emas balki kompyuterimiz xotirasiga saqlashni ko'rib chiqamiz.

MemoryStream ham "Stream" sinf(class)laridan biri bo'lib, bu sinf(class) ham System.IO nomlar makoni(namespace)da joylashgan. Uning koddagi ko'rinishi(syntax) MemoryStream ni undan obyekt olib ishlatamiz :

```csharp
MemoryStream memStream = new MemoryStream();
```

Nomidan ham ko'rinib turibdiki, u to'g'ridan-to'g'ri xotiradagi ma'lumotlar bilan shug'ullanadi, ya'ni ma'lumotlarimizni kompyuterning tezkor xotirasi(RAM)da vaqtinchalik saqlab turadi. Uning ustunligi tezkorligidadir. Agar biz ma'lumotlarga ko'plab murojaat qiladigan bo'lsak MemoryStream dan foydalanish afzaldir chunki, MemoryStreamdagi baytlar diskda emas, balki xotirada saqlanadi. Keling bir misolda ko'rib chiqamiz. O'zimiz bilgan StreamWriter orqali oddiy matn fayliga "Salom, Dunyo !!!" deb yozamiz va uning barcha baytlarni byte array orqali o'qiymiz. Keyin biz ushbu saqlangan baytlar bilan MemoryStream yaratamiz va MemoryStream dan barcha satrlarni o'qiy oladigan o'zimiz bilgan StreamReader yaratamiz.

```csharp
using System;
using System.IO;
using System.Text;

class MemStream
{
    static void Main()
    {
        StreamWriter writer = new StreamWriter("biror.txt");
        writer.WriteLine("Salom, Dunyo !!!");
        writer.Close();
        
        byte[] bytesAll = File.ReadAllBytes("biror.txt");

        MemoryStream memoryStream = new MemoryStream(bytesAll);

        StreamReader stream = new StreamReader(memoryStream);  
     
                string line;
                while ((line = stream.ReadLine()) != null)
                    Console.WriteLine(line);
            stream.Close();
        
        memoryStream.Close();
    }
}
```

Output:`Salom, Dunyo !!!`

Har bir stream ni tugatish uchun "close" ning o'rniga "using" kalit so'zi bilan blokdan foydalanishingiz mumkin (1=2).

1.

```csharp
MemoryStream memoryStream = new MemoryStream();
...... 

memoryStream.Close();
```

2.

```csharp
using(MemoryStream memoryStream = new MemoryStream())
{
......
}
```

{% hint style="info" %}
MUHIM! MemoryStream dan har doim ham foydalanish tavsiya berilmaydi, chunki :

1. Ma'lumotlar hajmi katta bo'lsa RAM ga sig'may qolishi mumkin;
2. MemoryStreamda saqlagan ma'lumotlar dastur yopilganda yoki kompyuter o'chiib yoqilishi kabi holatlarda yo'qolib qolishi ya'ni xotiradan o'chib ketishi mumkin.
   {% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.dot-net.uz/c-.net/basic/yuqori-daraja/stream-i-o/memorystream.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
