Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
AnswerRe: Python httpServ.request in C# - HttpWebRequest ? Pin
Richard MacCutchan30-Sep-18 21:08
mveRichard MacCutchan30-Sep-18 21:08 
GeneralRe: Python httpServ.request in C# - HttpWebRequest ? Pin
Member 140028821-Oct-18 11:26
Member 140028821-Oct-18 11:26 
QuestionSwitching 2 Variables Pin
Member 1400283730-Sep-18 8:19
Member 1400283730-Sep-18 8:19 
AnswerRe: Switching 2 Variables Pin
Dave Kreskowiak30-Sep-18 8:53
mveDave Kreskowiak30-Sep-18 8:53 
GeneralRe: Switching 2 Variables Pin
Member 1400283730-Sep-18 10:55
Member 1400283730-Sep-18 10:55 
AnswerRe: Switching 2 Variables Pin
BillWoodruff14-Oct-18 15:35
professionalBillWoodruff14-Oct-18 15:35 
QuestionFundamental misunderstanding of structs as "value" types Pin
bh_28-Sep-18 3:54
bh_28-Sep-18 3:54 
AnswerRe: Fundamental misunderstanding of structs as "value" types Pin
OriginalGriff28-Sep-18 4:12
mveOriginalGriff28-Sep-18 4:12 
Arrays of anything are always reference types, even if the base element is a value type. So when you copy the struct, you will copy the reference. Take a simple struct:
private struct MyStruct
    {
    public char X;
    public int[] Ia;
    public MyStruct(char x, int[] ia)
        {
        X = x;
        Ia = ia;
        }
    }

MyStruct ms1 = new MyStruct('1', new int[1] {101});
MyStruct ms2 = ms1;
Console.WriteLine("{0}:{1}", ms1.X, ms1.Ia[0]);
Console.WriteLine("{0}:{1}", ms2.X, ms2.Ia[0]);
Understandably, you get:
1:101
1:101
But, they are independent from that point on:
MyStruct ms1 = new MyStruct('1', new int[1] {101});
MyStruct ms2 = ms1;
Console.WriteLine("{0}:{1}", ms1.X, ms1.Ia[0]);
Console.WriteLine("{0}:{1}", ms2.X, ms2.Ia[0]);
ms2.Ia = new int[1] { 202 };
Console.WriteLine("{0}:{1}", ms1.X, ms1.Ia[0]);
Console.WriteLine("{0}:{1}", ms2.X, ms2.Ia[0]);

Will give you
1:101
1:101
1:101
1:202
As you would expect.
But you can't expect the copy to create new reference objects becasue the new keyword is never used:
MyStruct ms1 = new MyStruct('1', new int[1] {101});
MyStruct ms2 = ms1;
Console.WriteLine("{0}:{1}", ms1.X, ms1.Ia[0]);
Console.WriteLine("{0}:{1}", ms2.X, ms2.Ia[0]);
ms2.Ia[0] = 202;
Console.WriteLine("{0}:{1}", ms1.X, ms1.Ia[0]);
Console.WriteLine("{0}:{1}", ms2.X, ms2.Ia[0]);
Will give you what you are seeing:
1:101
1:101
1:202
1:202
Because arrays are reference types and the struct copy will copy the reference to the array - which is exactly what it should do!

What you are looking for is a deep copy of the struct, which is not possible using "normal" operations - that's why structs should be immutable! Choosing Between Class and Struct | Microsoft Docs[^]
Quote:
CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.


AVOID defining a struct unless the type has all of the following characteristics:

It logically represents a single value, similar to primitive types (int, double, etc.).
It has an instance size under 16 bytes.
It is immutable.
It will not have to be boxed frequently

This may help: Using struct and class - what's that all about?[^]
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: Fundamental misunderstanding of structs as "value" types Pin
bh_28-Sep-18 4:49
bh_28-Sep-18 4:49 
GeneralRe: Fundamental misunderstanding of structs as "value" types Pin
OriginalGriff28-Sep-18 4:55
mveOriginalGriff28-Sep-18 4:55 
Questionhow to create discussion forum in our asp.net application Pin
Member 1400092528-Sep-18 2:29
Member 1400092528-Sep-18 2:29 
AnswerRe: how to create discussion forum in our asp.net application Pin
Pete O'Hanlon28-Sep-18 3:24
mvePete O'Hanlon28-Sep-18 3:24 
Questionmake a design similar to windows mail in visual studio Pin
erdalczr28-Sep-18 0:46
erdalczr28-Sep-18 0:46 
AnswerRe: make a design similar to windows mail in visual studio Pin
OriginalGriff28-Sep-18 2:35
mveOriginalGriff28-Sep-18 2:35 
GeneralRe: make a design similar to windows mail in visual studio Pin
Richard MacCutchan28-Sep-18 3:29
mveRichard MacCutchan28-Sep-18 3:29 
GeneralRe: make a design similar to windows mail in visual studio Pin
OriginalGriff28-Sep-18 3:54
mveOriginalGriff28-Sep-18 3:54 
QuestionUploading a large Excel file using Entity Framework and Stored Procedure Call Pin
simpledeveloper27-Sep-18 8:40
simpledeveloper27-Sep-18 8:40 
AnswerRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
Dave Kreskowiak27-Sep-18 9:50
mveDave Kreskowiak27-Sep-18 9:50 
GeneralRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
simpledeveloper27-Sep-18 10:11
simpledeveloper27-Sep-18 10:11 
GeneralRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
Member 140028821-Oct-18 0:10
Member 140028821-Oct-18 0:10 
GeneralRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
Dave Kreskowiak1-Oct-18 4:20
mveDave Kreskowiak1-Oct-18 4:20 
GeneralRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
Member 140028821-Oct-18 11:27
Member 140028821-Oct-18 11:27 
GeneralRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
simpledeveloper1-Oct-18 13:30
simpledeveloper1-Oct-18 13:30 
GeneralRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
Dave Kreskowiak1-Oct-18 15:52
mveDave Kreskowiak1-Oct-18 15:52 
GeneralRe: Uploading a large Excel file using Entity Framework and Stored Procedure Call Pin
simpledeveloper2-Oct-18 7:58
simpledeveloper2-Oct-18 7:58 

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.