Difference between struct and class in C #

Source: Internet
Author: User
1. struct does not allow display and declaration of its non-parameter constructor, which is different from the class
2. When struct cannot be declared, initialize its data member Value
3. When struct is passed as a parameter, you can consider using ref to optimize performance: because it is a value type (but pay attention to its value change)
4. struct does not inherit, But it inherits from system. valuetype ----> system. Object
5. struct can be viewed as a narrow class, which is suitable for small data members.
6. Understand as follows: Code : Class Class1
{
[Stathread]
Static   Void Main ( String [] ARGs)
{
Dimensions point =   New Dimensions ();
Console. writeline (point );

Dimensions point1;
Point1.length =   100 ;
Point1.width =   200 ;
Console. writeline (point1 );

Console. Readline ();
}
}

Public   Struct Dimensions
{
Public   Double Length;
Public   Double Width;

Public   Override   String Tostring ()
{
Return This. Length+ ":" + This. Width;
}
}

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.