DEV Community

asilbek ibragimov
asilbek ibragimov

Posted on

2 2 2 2 2

27.Value Type va Reference Type (Advanced)

27. Value Type va Reference Type (Advanced)
a) C# da struct va class orasidagi asosiy farq nimada?
b) Quyidagi kod qanday natija beradi?

struct Point
        { 
            public int X;
            public int Y;
        }
        Point p1 = new Point():
        p1.X = 10;
        p1.Y = 20;
        Point p2 = p1;
        p2.X = 30;
        Console. WriteLine(p1.X);
Enter fullscreen mode Exit fullscreen mode

Javoblari:

   a) Struct - bu value type. U stack xotirasida saqlanadi va 
   qiymatni o'zlashtirganda nusxasi o'tkaziladi.
   Class - bu reference type. U heap xotirasida saqlanadi va 
   qiymatni o'zlashtirganda faqat reference (manzil) nusxa 
   qilinadi.
   b) bu yerda p1.X ning qiymati chiqariladi, va bu qiymat 10 
   bo‘ladi, chunki p1.X ning qiymati o‘zgarmagan.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay