简体   繁体   中英

C# Struct array but not class array

I was able to create an array of a struct I created, but I'm having trouble doing the same for an array of a class . I'm (faintly) aware that this probably isn't the best way to do this , but I'd appreciate help in figuring out what's going on.

I'm about 2 days into learning C#, and I'm navigating away from MS Office-VBA, if that gives you an idea of what I'm into. Anyway, I'm following an online reference , and along the way trying to play with what I've learned so far. This problem has come about as a result of my playing.

First, let me describe what I've done with the struct, and the array of that struct, with some code snippets.

I've been able to create a struct , called Machines...

// play with structs
struct Machines
{
    // vars for struct
    private string model, SN;
    private int hours;

    // assign values
    public void AssignValues(string model_in, string SN_in, int hours_in)
    {
        model = model_in;
        SN = SN_in;
        hours = hours_in;
    }

    // display values
    public void DisplayValues()
    {
        Console.WriteLine("Model: {0}", model);
        Console.WriteLine("SN: {0}", SN);
        Console.WriteLine("Hours: {0}", hours);
    }
};

... things seem to work just fine:

public static void Main()
{
    // play with structures
    Machines machine1 = new Machines();
    machine1.AssignValues("AA", "ABC01234", 34760);
    machine1.DisplayValues();

Output is:

Model: AA
SN: ABC01234
Hours: 34760

Then, I can create an array of the struct, and things continue to go well:

// play with structures and arrays
// declare, create new instance
Machines [] MyArr = new Machines[10];
MyArr[0].AssignValues("AA", "ABC01235", 43000);
MyArr[0].DisplayValues();

But, when I attempt to do the same with a class , it's a different story. What's going on?

public class ArmstrongMachine
{
    // vars for struct
    private string model, SN;
    private int hours;

    // assign values
    public void AssignValues(string model_in, string SN_in, int hours_in)
    {
        model = model_in;
        SN = SN_in;
        hours = hours_in;
    }

    // display values
    public void DisplayValues()
    {
        Console.WriteLine("Model: {0}", model);
        Console.WriteLine("SN: {0}", SN);
        Console.WriteLine("Hours: {0}", hours);
    }
};

...

    // play with classes
    ArmstrongMachine [] MyMachines = new ArmstrongMachine[10];
    MyMachines[0].AssignValues("AA", "ABC01236", 51000);
    MyMachines[0].DisplayValues();

The issue seems to begin with MyMachines[0].AssignValues... . If I comment out that line and the following, there are no problems (other than the warning that I've created a variable I'm not using).

Any ideas?

Also, please be aware that this is being compiled online .

A class gives you a reference type in C#.

The array thus holds references to objects, and not the objects themselves.

The array initially contains nothing, all zeroes, which means all the references will be null .

You need to initialize each element of the array to hold an object reference:

// play with classes
ArmstrongMachine [] MyMachines = new ArmstrongMachine[10];
MyMachines[0] = new ArmstrongMachine();
MyMachines[0].AssignValues("AA", "ABC01236", 51000);
MyMachines[0].DisplayValues();
MyMachines[1] = new ArmstrongMachine();
MyMachines[2] = new ArmstrongMachine();
...
MyMachines[9] = new ArmstrongMachine();

If the array holds value types, like the structs, then the array holds the struct values themselves, thus it works with the structs, and not with the objects.

Also note that you should emphatically not use mutable structs (structs you can change). There's tons of things that can go wrong and bite you in ... so you should not use them. Go with classes in this case.

Here's a video on the subject of mutable structs: Evil Structs, by Jon Skeet .

In simple words: A struct is only a way the memory is structured whereas a class is a real object. For the second example, you need to create instances of the class before you can make calls to it, because it only points to a memory location (which may be unassigned = a null reference):

ArmstrongMachine [] MyMachines = new ArmstrongMachine[10];
MyMachines[0] = new ArmstrongMachine();
MyMachines[0].AssignValues("AA", "ABC01236", 51000);
MyMachines[0].DisplayValues();

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