About structs in C # (bottom)

Source: Internet
Author: User
The previous article briefly describes the performance, limitations and comparisons of the structure with the class, and I will go on to explain the principles that should be taken into account when using structs:
Through the introduction of the previous article, we can naturally realize the superiority of the structure in efficiency (relative to the Class), which is mainly due to their underlying value type structure.
However, their limitations on the processing of large-capacity data and complex algorithms make the scope of its application very limited. Then what are we feeling?
The use of structure can not be blamed and ridiculed?
1, if you are engaged in image color or fixed-mode business process design, or you design object group, you need to face a large number of simple structure and state
With less information, I recommend that you use the structure type to construct these small data bodies.
2, because the structure of the prototype is a value type, so it is defined as a data, so do not try to construct too many methods in the structure, preferably can not define the side
Law, try to avoid it.

Let's look at one of the most representative examples offered by Microsoft: the RGB structure
Using System;

<summary>
RGB structure
</summary>
struct RGB
{
public static readonly RGB RED = new RGB (255,0,0);
public static readonly RGB GREEN = new RGB (0,255,0);
public static readonly RGB BLUE = new RGB (0,0,255);
public static readonly RGB white = new RGB (255,255,255);
public static readonly RGB black = new RGB (0,0,0);

public int Red;
public int Green;
public int Blue;
Public RGB (int red,int green,int blue)
{
red = red;
Green = green;
blue = blue;
}
public override string ToString ()
{
Return (red.tostring ("X2") + green.tostring ("X2") + blue.tostring ("X2"));
}
}

public class Struct
{
static void Outputrgbvalue (String Color,rgb RGB)
{
Console.WriteLine ("The Value for {0} is {1}", Color,rgb);
}

static void Main (string[] args)
{
Outputrgbvalue ("Red", RGB. RED);
Outputrgbvalue ("Green", RGB. GREEN);
Outputrgbvalue ("Blue", RGB. BLUE);
Outputrgbvalue ("White", RGB. White);
Outputrgbvalue ("Black", RGB. Black);
}
}
In the example above we have defined a structure and a static field, which improves the efficiency of our storage and facilitates the user, after all, remembering a
RGB (255,100,255) is more difficult than remembering a "grass shallow orchid"; Because of the joining of static members, so that each RGB key value for the entire system to use only the definition
At once, the efficiency and convenience of this use is obvious.



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.