C # differences between classes and structures

Source: Internet
Author: User

Differences between classes and structures
The only difference between the structure and class in C ++ is the default access level, but the difference in C # Is A Little More. in C #, it is as follows:
(1) the class is a reference type, and the structure is a value type.
Class:

public class Person{    public string Name { get; set; }    public int Age { get; set; }    public Person(string name, int age)    {        Name = name;        Age = age;    }}class Program{    static void Main()    {        Person person1 = new Person("Leopold", 6);        Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);        // Declare  new person, assign person1 to it.        Person person2 = person1;        //Change the name of person2, and person1 also changes.        person2.Name = "Molly";        person2.Age = 16;        Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age);        Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);        // Keep the console open in debug mode.        Console.WriteLine("Press any key to exit.");        Console.ReadKey();    }}


Output:
Person1 Name = Leopold Age = 6
Person2 Name = Molly Age = 16
Person1 Name = Molly Age = 16

Structure:

public struct Person{    public string Name;    public int Age;    public Person(string name, int age)    {        Name = name;        Age = age;    }}public class Application{    static void Main()    {        // Create  struct instance and initialize by using "new".        // Memory is allocated on thread stack.        Person p1 = new Person("Alex", 9);        Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);        // Create  new struct object. Note that  struct can be initialized        // without using "new".        Person p2 = p1;        // Assign values to p2 members.        p2.Name = "Spencer";        p2.Age = 7;        Console.WriteLine("p2 Name = {0} Age = {1}", p2.Name, p2.Age);        // p1 values remain unchanged because p2 is  copy.        Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);        // Keep the console open in debug mode.        Console.WriteLine("Press any key to exit.");        Console.ReadKey();    }}
Output:
P1 Name = Alex Age = 9
P2 Name = Spencer Age = 7
P1 Name = Alex Age = 9
(2) The structure can contain constructor, but the constructor must assign values to all fields. Therefore, the structure does not have the default constructor.
(3) The structure cannot be inherited, but the structure can implement interfaces.
(4) The structure is directly inherited from System. ValueType, and the class is inherited from System. Object.
(5) Unless it is a const or static variable in the structure, or the variable cannot be initialized directly.
namespace ConsoleApplication5{    struct struct1    {        //int x = 10;//error        int x;        string str;        //struct1(){}//error        //struct1(int _x)//error        //{ x = _x; }        struct1(int _x, string _str)        {            x = _x;            str = _str;        }        const int y = 10;//OK        static int z = 100;//OK    }}

(6) The structure can be of the Nullable type.
The so-called Nullable type is System. Nullable. The value range is all valid values expressed by value type T plus a null value.
The meaning of Nullable: The value type must be initialized before it can be used. In order to prevent the value type from being initialized, this type is generated.

Related Article

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.