Click here to Skip to main content
15,867,906 members
Home / Discussions / C#
   

C#

 
GeneralRe: Struct vs Class? Pin
harold aptroot16-Feb-14 22:02
harold aptroot16-Feb-14 22:02 
GeneralRe: Struct vs Class? Pin
OriginalGriff16-Feb-14 22:17
mveOriginalGriff16-Feb-14 22:17 
GeneralRe: Struct vs Class? Pin
OriginalGriff16-Feb-14 22:26
mveOriginalGriff16-Feb-14 22:26 
GeneralRe: Struct vs Class? Pin
harold aptroot16-Feb-14 22:59
harold aptroot16-Feb-14 22:59 
GeneralRe: Struct vs Class? Pin
OriginalGriff17-Feb-14 0:06
mveOriginalGriff17-Feb-14 0:06 
GeneralRe: Struct vs Class? Pin
harold aptroot17-Feb-14 0:16
harold aptroot17-Feb-14 0:16 
GeneralRe: Struct vs Class? Pin
jschell17-Feb-14 12:07
jschell17-Feb-14 12:07 
GeneralRe: Struct vs Class? Pin
BillWoodruff17-Feb-14 1:41
professionalBillWoodruff17-Feb-14 1:41 
+5 very useful response !

I find it hard to conceptualize structs as "immutable" when I can do stuff like that shown in the code for 'TestStruct below; and, so do other people:
C#
public struct MyStruct
{
    public string Name;
    public int X;
    public int Y;

    public int Z { set; get; }

    public MyStruct(int z) : this() { Z = z; }
}

public void increment(ref MyStruct aStruct, int inc )
{
    aStruct.X += inc;
}

private void WriteSValues(params MyStruct[] theStructs)
{
    foreach (var theStruct in theStructs)
    {
        Console.WriteLine("Name: {0} X: {1} Y: {2} X: {3}",
            theStruct.Name,
            theStruct.X,
            theStruct.Y,
            theStruct.Z);
    }
    Console.WriteLine();
}

// test
private void TestStruct()
{
    MyStruct ms1 = new MyStruct(300) {Name = "ms1", X = 100, Y = 200};
    ms1.X += -300;

    MyStruct ms2 = ms1;
    ms2.Name = "ms2";

    WriteSValues(ms1, ms2);

    increment(ref ms1, 200);

    WriteSValues(ms1, ms2);

    ms1.X += 500;
    WriteSValues(ms1, ms2);

    MyStruct ms3 = ms2;
    ms3.Name = "ms3";
    WriteSValues(ms1, ms2, ms3);

    increment(ref ms3, 1000);
    WriteSValues(ms1, ms2, ms3);
}
The above is a diagnostic quiz I wrote for some supposedly "intermediate" C# students (not students of mine): their task was to describe the values for each struct created, at each line of the code, and explain why the values were what they were. About half understood that assigning an instance of a struct to another instance of a struct of the same Type created a copy, but they all were quite confused by the other bits in the code. The quiz was administered by their regular teacher, so I don't think the results exhibit bias due to shyness in the presence of someone from outside their social process.
“But I don't want to go among mad people,” Alice remarked.

“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”

“How do you know I'm mad?” said Alice.

“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll

AnswerStruct vs Class? (Off Topic) Pin
OriginalGriff17-Feb-14 0:41
mveOriginalGriff17-Feb-14 0:41 
GeneralRe: Struct vs Class? (Off Topic) Pin
David C# Hobbyist.17-Feb-14 11:12
professionalDavid C# Hobbyist.17-Feb-14 11:12 
GeneralRe: Struct vs Class? (Off Topic) Pin
OriginalGriff17-Feb-14 11:24
mveOriginalGriff17-Feb-14 11:24 
GeneralRe: Struct vs Class? (Off Topic) Pin
OriginalGriff23-Feb-14 0:29
mveOriginalGriff23-Feb-14 0:29 
GeneralRe: Struct vs Class? (Off Topic) Pin
David C# Hobbyist.23-Feb-14 4:42
professionalDavid C# Hobbyist.23-Feb-14 4:42 
GeneralRe: Struct vs Class? (Off Topic) Pin
OriginalGriff23-Feb-14 4:57
mveOriginalGriff23-Feb-14 4:57 
QuestionInterface for Authorization Custom Inteface c# Winforms .net 4.0 Pin
Member 1050351416-Feb-14 12:10
Member 1050351416-Feb-14 12:10 
AnswerRe: Interface for Authorization Custom Inteface c# Winforms .net 4.0 Pin
Eddy Vluggen17-Feb-14 0:29
professionalEddy Vluggen17-Feb-14 0:29 
QuestionStrange bug when converting from binary to decimal.. Pin
Alex Reed16-Feb-14 9:54
Alex Reed16-Feb-14 9:54 
AnswerRe: Strange bug when converting from binary to decimal.. Pin
Richard Andrew x6416-Feb-14 11:25
professionalRichard Andrew x6416-Feb-14 11:25 
GeneralRe: Strange bug when converting from binary to decimal.. Pin
Alex Reed17-Feb-14 14:01
Alex Reed17-Feb-14 14:01 
AnswerRe: Strange bug when converting from binary to decimal.. Pin
Richard MacCutchan16-Feb-14 22:40
mveRichard MacCutchan16-Feb-14 22:40 
GeneralRe: Strange bug when converting from binary to decimal.. Pin
Richard Deeming17-Feb-14 1:54
mveRichard Deeming17-Feb-14 1:54 
GeneralRe: Strange bug when converting from binary to decimal.. Pin
Richard MacCutchan17-Feb-14 3:01
mveRichard MacCutchan17-Feb-14 3:01 
GeneralRe: Strange bug when converting from binary to decimal.. Pin
Alex Reed17-Feb-14 14:00
Alex Reed17-Feb-14 14:00 
GeneralRe: Strange bug when converting from binary to decimal.. Pin
Richard MacCutchan17-Feb-14 21:12
mveRichard MacCutchan17-Feb-14 21:12 
Questiontabcontrol OwnerDrawFixed right to left in c# Pin
Member 1059114816-Feb-14 1:39
Member 1059114816-Feb-14 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.