[C #20] 1. When to use class and struct

Source: Internet
Author: User
Points

Struct is a value type that can contain data and functions.

Struct is a value type, so no heap is required. Instead, space is allocated on the stack.

Struct directly stores data in struct, while class only stores pointer of reference type

Struct applies to small data structures

Struct affects performance

Struct can use the new operation to call the constructor, but does not allocate memory on heap.

The struct constructor returns only the struct value itself (usually allocated on the stack)

When using class, multiple variables can reference the same object.

Each sturct variable is used to save its own data copies without affecting each other.

Struct does not support inheritance. sturct inherits from object type.

Demo
    class Program    {        class PointClass        {            public int x;            public int y;            public PointClass(int x, int y)            {                this.x = x;                this.y = y;            }        }        struct PointStruct        {            public int x;            public int y;            public PointStruct(int x, int y)            {                this.x = x;                this.y = y;            }        }        static void Main(string[] args)        {            PointStruct pointStruct = new PointStruct(10, 10);            Console.WriteLine("Initial struct values are {0},{1}", pointStruct.x, pointStruct.y);            ModifyStructPoint(pointStruct);            Console.WriteLine("After ModifyStructPoint, struct values are {0},{1}", pointStruct.x, pointStruct.y);            Console.WriteLine();            PointClass pointClass = new PointClass(10, 10);            Console.WriteLine("Initial Class values are {0},{1}", pointClass.x, pointClass.y);            ModifyClassPoint(pointClass);            Console.WriteLine("After ModifyClassPoint, class values are {0},{1}", pointClass.x, pointClass.y);            Console.ReadLine();        }        private static void ModifyStructPoint(PointStruct pointStruct)        {            pointStruct.x = 20;            pointStruct.y = 20;            Console.WriteLine("Modified Valuesare {0},{1}", pointStruct.x, pointStruct.y);        }        private static void ModifyClassPoint(PointClass pointClass)        {            pointClass.x = 20;            pointClass.y = 20;            Console.WriteLine("Modified Valuesare {0},{1}", pointClass.x, pointClass.y);        }    }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.