The relationship of C # classes and structs

Source: Internet
Author: User

Primary Contact: Classes and structs are templates that create objects, each of which contains data and provides methods for processing and accessing the data. class defines what data and functionality each class object (instance) can contain. For example, if a class represents a customer, you can define fields CustomerID, FirstName, LastName, and address, which contain information about the customer. You can also define the ability to process the data stored in these fields. You can then instantiate the object of this class to represent a customer and set these fields for this instance, using its capabilities.

 class   Phonecustomer { public  const  string  Dayofsendingbill =   " ;  public  int          CustomerID;  public  string   FirstName;  public  string   LastName; }

The main difference: the difference between structs and classes is how they are stored in memory (classes are reference types stored on heaps (heap), structures are value types stored on stacks), access methods, and some characteristics (such as structs do not support inheritance). Using structs for smaller data types can improve performance. But syntactically, structs and classes are very similar, and the main difference is that they use the keyword struct instead of class to declare the structure. For example, if you want all Phonecustmer instances to be stored on the stack, rather than on the managed heap, you can write the following statement:

struct phonecustomerstruct    {        publicconststring"Monday";          Public int CustomerID;          Public string FirstName;          Public string LastName;    }

For classes and structs, you use the keyword new to declare an instance: This keyword creates an object and initializes it. In most cases, classes are much more common than structs.

Newnew phonecustomerstruct ();

The relationship of C # classes and structs

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.