DEV Community

Cover image for Stack and Heap
Sabrina
Sabrina

Posted on

Stack and Heap

is string literal mutable or immutable ?

is string variable mutable or immutable ?

string literal - deb " " ichida constanta sifatida yozilgan stringga aytiladi.
string literal immutable - degani bu qiymatni o'zgaritirib bo' lmaydi.

var name = "Ismalali Ustoz";
name = "islamali ustoz";
var son = 15;
son = 16;
Console. WriteLine (name);
Console. WriteLine(son);
Enter fullscreen mode Exit fullscreen mode

Value typelar - stackda saqlanadi
Value type **_deganda, ma' lumotlar tog'ridan- to'g'ri o'zgaruvchida saqlanadi. Bunda o'zgaruvchining qiymati o'zi bilan birga saqlanadi.
_
Value typelar**
stack xotirasida saqlanadi.
Value typega quydagilar kiradi:
Oddiy turlar - int, float, double, char,bool va h.k;
struct, enum;

int a = 5;
int b = a;
b = 10;
Console.WriteLine(a);
Console-WriteLine(b);
Enter fullscreen mode Exit fullscreen mode

Reference typelar - heapda sagalanadi
Reference type deganda, o' zgaruvchi faqatgina ob'ektning manzilini (reference) saqlaydi, ob'ektning o'zi esa heap (xotiradi) saqlanadi.
Reference typega quydagilar kiradi - class, interface, array(massivlar), delegate.

Stack xotira: Bu juda tez va kichik xotira bo'lib, u qiymatlarni to'g'ridan-to'g'ri o' zgaruvchida saqlaydi.
Value type o' zgaruvchilari stackda saqlanadi.
Stackda saqlangan qlymatlar to'g'ridan-to'g'ri foydalaniladi va o'chiriladi, shuning uchun stack xotira juda samarali.
Heap xotira: Bu katta hajmdagi va nisbatan sekinrog xotira bo'lib, unda ma' lumotlar dinamik ravishda yaratiladi va boshgariladi.
Reference type o' zgaruvchilari heapda saqlanadi. Heapda ma' lumotlar ko'proq joyni talab qiladi, va ular stackdan farqli o'laroq, murakkabroq boshgariladi (masalan, garbage collection orgali).

string concatenation => stringlarni bir biriga qo'shish

var greeting = "Hello" + " " + name + " ";
Console. WriteLine(greeting);
Enter fullscreen mode Exit fullscreen mode

Agar space holder ichida o' zgaruvchidan keyin, 10 soni yoziladigan bo'lsa bizga consoledan 10 ta joy ajratib beradi.
Agar -10 yoziladigan bo' lsa chap tarfdan rostlab chop etadi.
var butunSon = 2;

Console. WriteLine($"Kiritilgan butunSon: {butunSon,
10: D4}");
Console. WriteLine($"Kiritilgan butunSon: {butunSon, -10:D4}");
Enter fullscreen mode Exit fullscreen mode
string
firstFriend = "Sunnat";
string
secondFriend
= "Firdavs";
Console WriteLine($"The name (firstFriend) and {secondFriend}");
Enter fullscreen mode Exit fullscreen mode

//Length - function tekstni uzinligini aniqlab beradi, o'zgaruvchidan keyin Length shaklida yoziladi.

Console WriteLine($"The name {firstFriend} has {firstFriend.Length) letters");
Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters");
var carModel = "BYD Song L";
Console-WriteLine("String = (0}, Length = {1}", carModel, carModel.Length);
for int
1 = 0; 1 < carModel. Length; i++)
Console.WriteLine(carModel [i]) ;
Enter fullscreen mode Exit fullscreen mode

//String metodlari
//TrimStart() - function tekstni oldidagi joylarni olib tashlaydi.
// TrimEnd() - function tekstni oxiridagi joylarni olib tashlaydi
//Trim() - fucntion oldi va oxiridagi joylarni tengdaniga olib tashlaydi.

string greeting2 = "
Hello
Console. WriteLine(greeting2);
string
trimmedGreeting = greeting2. TrimStart();
Console. WriteLine (trimmedGreeting) ;
trimmedGreeting = greeting2.TrimEnd();
Console.WriteLine(trimmedGreeting) ;
trimmedGreeting = greeting2. Trim();
Console. WriteLine(trimmedGreeting);
Enter fullscreen mode Exit fullscreen mode

//Replace() →> function tekst qiymatini o'rniga yangi qiymatga o'zgartirrish imkoniyatini beradi.

string sayHello= "Hello World";
Console.WriteLine(sayHello) ;
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);

Enter fullscreen mode Exit fullscreen mode

//ToUpper () -> function xamma harflarni kotta qilib beradi.
//ToLower () -> fucntion xamma harflarni kichik qilib beradi.

Console.WriteLine(sayHello. ToUpper());
Console.WriteLine(sayHello. ToLower());
Enter fullscreen mode Exit fullscreen mode

//Escape character && verbatim string
// escape character maxsus belgilarni chop etishda backslashda foydalanishimiz kerak - 1

var specialString = "U judayam \"zor\" bola";
Console. WriteLine(specialString);
Console-WriteLine("olma\nanor \nnok");
Console. WriteLine("olma\ tanor\tnok");
Console-WriteLine("olma \banor");
Console-WriteLine("olma\ \anor\ \nok");
Console.WriteLine("C: \ \Windows\ \System32\\calc.exe");
Console-WriteLine(@"C: \Windows\System32\calc.exe");
Console-WriteLine("C: \Windows\nSystem32\ncalc.exe");
Console-WriteLine(@"C: \nWindows\nSystem32\ncalc-exe");
Enter fullscreen mode Exit fullscreen mode

//Contains() - checks whether the string contains a substring

string songLyrics = "You say goodbye, and I say hello";
Console. WriteLine (songLyrics.Contains ("goodbye") );
Console. WriteLine(songLyrics.Contains ("say"));
Console-WriteLine (songLyrics.Contains ("greetings")):
//StartWith() - checks if the string begins with the given string
Console. WriteLine (songLyrics.StartsWith ("You"));
Console WriteLine(songLyrics-StartsWith ("goodbye")) ;
//EndWith() → checks if the string ends with the given string
Console-WriteLine(songLyrics.EndsWith("hello"));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)