Click here to Skip to main content
15,889,682 members
Home / Discussions / C#
   

C#

 
AnswerRe: Mysql database freezes with Visual Sudio C# Pin
RedDk18-Dec-22 10:10
RedDk18-Dec-22 10:10 
AnswerRe: Mysql database freezes with Visual Sudio C# Pin
Dave Kreskowiak18-Dec-22 11:20
mveDave Kreskowiak18-Dec-22 11:20 
AnswerRe: Mysql database freezes with Visual Sudio C# Pin
Richard Deeming18-Dec-22 21:08
mveRichard Deeming18-Dec-22 21:08 
QuestionMulti-threaded debugging Pin
Richard Andrew x6411-Dec-22 6:12
professionalRichard Andrew x6411-Dec-22 6:12 
AnswerRe: Multi-threaded debugging Pin
Gerry Schmitz11-Dec-22 7:09
mveGerry Schmitz11-Dec-22 7:09 
GeneralRe: Multi-threaded debugging Pin
Richard Andrew x6411-Dec-22 7:48
professionalRichard Andrew x6411-Dec-22 7:48 
GeneralRe: Multi-threaded debugging Pin
Gerry Schmitz11-Dec-22 14:50
mveGerry Schmitz11-Dec-22 14:50 
AnswerRe: Multi-threaded debugging Pin
Randor 11-Dec-22 8:46
professional Randor 11-Dec-22 8:46 
GeneralRe: Multi-threaded debugging Pin
Richard Andrew x6411-Dec-22 14:36
professionalRichard Andrew x6411-Dec-22 14:36 
PraiseRe: Multi-threaded debugging Pin
Randor 11-Dec-22 15:50
professional Randor 11-Dec-22 15:50 
QuestionRegarding a line in code Pin
vineeth s5-Dec-22 7:06
vineeth s5-Dec-22 7:06 
AnswerRe: Regarding a line in code Pin
Richard MacCutchan5-Dec-22 8:02
mveRichard MacCutchan5-Dec-22 8:02 
AnswerRe: Regarding a line in code Pin
OriginalGriff5-Dec-22 8:22
mveOriginalGriff5-Dec-22 8:22 
AnswerRe: Regarding a line in code Pin
BillWoodruff6-Dec-22 0:51
professionalBillWoodruff6-Dec-22 0:51 
GeneralRe: Regarding a line in code Pin
Richard Deeming6-Dec-22 1:08
mveRichard Deeming6-Dec-22 1:08 
GeneralRe: Regarding a line in code Pin
BillWoodruff6-Dec-22 1:29
professionalBillWoodruff6-Dec-22 1:29 
GeneralRe: Regarding a line in code Pin
Richard MacCutchan6-Dec-22 1:34
mveRichard MacCutchan6-Dec-22 1:34 
GeneralRe: Regarding a line in code Pin
BillWoodruff6-Dec-22 1:43
professionalBillWoodruff6-Dec-22 1:43 
GeneralRe: Regarding a line in code Pin
Richard MacCutchan6-Dec-22 2:03
mveRichard MacCutchan6-Dec-22 2:03 
QuestionMessage Closed Pin
29-Nov-22 0:17
zystein29-Nov-22 0:17 
AnswerRe: A Problem about Collections Pin
Richard MacCutchan29-Nov-22 0:50
mveRichard MacCutchan29-Nov-22 0:50 
AnswerRe: A Problem about Collections Pin
OriginalGriff29-Nov-22 1:47
mveOriginalGriff29-Nov-22 1:47 
To add to what Richard has said, the important thing here is the keyword new

When you create a new instance of any class, that is exactly what you get: a brand new, totally separate instance of the class which has nothing at all to do with any other. Think about it: if you put your mobile in the glovebox of your car, then go and buy a new vehicle, would your mobile be in it's glovebox? Of course not - you understand that the two cars are separate, and whatever you do to the first one has no affect whatsoever on the second! Indeed, life would get very odd if it was otherwise!

So when you do this:
C#
List<int> a = new List<int>();
List<int> b = new List<int>();
List<int> c = new List<int>();
you create three independent collections: whatever you do to one has no affect on the others. When you add items to a collection a "copy" of the value is added so when you remove them, only that local copy gets removed.

If instead of integers you used a class, then a copy of the reference to that class instance is added, so if a class exists in two collections, then changing it's content via either collection effects the same instance:
C#
public class MyClass
    {
    public int X { get; set; }
    }
private void MyButton_Click(object sender, EventArgs e)
    {
    List<MyClass> a = new List<MyClass>();
    List<MyClass> b = new List<MyClass>();
    MyClass mc = new MyClass() { X = 1 };
    a.Add(mc);
    b.Add(mc);
    foreach (MyClass m in a)
        {
        m.X += 10;
        }
    foreach (MyClass m in b)
        {
        m.X += 100;
        }
    Console.WriteLine(mc.X);
    }
Will print "111" because the same instance is accessed from both collections.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

QuestionSimple Broadcast Message on Local Host Pin
CodaNV28-Nov-22 21:49
CodaNV28-Nov-22 21:49 
AnswerRe: Simple Broadcast Message on Local Host Pin
OriginalGriff28-Nov-22 23:04
mveOriginalGriff28-Nov-22 23:04 
GeneralRe: Simple Broadcast Message on Local Host Pin
CodaNV28-Nov-22 23:34
CodaNV28-Nov-22 23:34 

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.