简体   繁体   中英

Confused about how C# struct work

I have a struct

public struct card
{
    PictureBox picture;
    double value;
}

I want to make an array of that, and Add/remove pictures and value as I go on. I'm not able to do this

card[] c = new card[13];
c[1].value = 4; 

How do assign, read, and chance values of the those?

Make value public .

public double value;

By default , class/struct level elements are private , which makes it inaccessible.

It is recommended to capitalize public elements, and to use properties instead of fields, so using the following would be better:

public double Value { get; set; }

You may want to consider making your struct a class , as is not a very good fit. (See When to use struct? , most of the time you will be working with classes in C#) You could also use a dictionary of picture's and their values:

public Dictionary<Picture, double> Cards;

A struct in C# is not equivalent to a C struct. In C#, a struct has a by value copy semantic. This can be a bit confusing.

Consider the following:

Card[] cards = new Card[13];
Card card = cards[1];
card.Value = 42;

You would probably expect cards[1].Value to be 42, but you'll be surprised when you find out it isn't. This is partially the reason why mutable structs are evil .

Go with a class instead, which is closer to a C struct in the way that a copy of the reference to the class is passed, instead of copying the value itself:

public class Card
{
    public PictureBox Picture { get; set; }
    public double Value { get; set; }
}

A structure is a bunch of variables stuck together with duct tape. If you want a bunch of independent-but-related variables stuck together with duct tape, it is often better to use a struct that exposes a bunch of variables stuck together with duct tape than to design a class which tries to serve that purpose, or a struct which pretends to be an object of a class which tries to serve that purpose. If, however, you want something that behaves as an object, then you should use a class.

Given card defined as shown, card[] foo = new card[10] will make foo identify an array holding ten references of type PictureBox and ten double values. The declaration card bar; will define space for another PictureBox reference and another double which are independent of anything else in the universe. Saying bar = foo[3]; will be equivalent to bar.picture = foo[3].picture; bar.value = foo[3].value; bar.picture = foo[3].picture; bar.value = foo[3].value; , and will not establish any lasting relationship between the fields of bar and those of foo[3] .

Duct-taped-variable structures can be very useful for some purposes, such as holding the coordinates of a point, but it is important to recognize them for what they are. If you want a type to represent an object, use a class. Only if you want it to hold a bunch of independent but related variables duct-taped together should you use a struct .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM