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

C#

 
QuestionHelp C# and Entity Developers Devart Pin
Alfredo Mendoza 31-Jul-17 19:18
Alfredo Mendoza 31-Jul-17 19:18 
AnswerRe: Help C# and Entity Developers Devart Pin
Pete O'Hanlon31-Jul-17 19:47
mvePete O'Hanlon31-Jul-17 19:47 
QuestionEarly session timeout in asp.net for multiple sessions Pin
punit.vk30-Jul-17 18:04
punit.vk30-Jul-17 18:04 
AnswerRe: Early session timeout in asp.net for multiple sessions Pin
OriginalGriff30-Jul-17 20:25
mveOriginalGriff30-Jul-17 20:25 
JokeC# WeakClone and DeepClone Pin
Jeong, WonYoung(Brad)28-Jul-17 20:12
professionalJeong, WonYoung(Brad)28-Jul-17 20:12 
GeneralRe: C# WeakClone and DeepClone Pin
Pete O'Hanlon29-Jul-17 3:23
mvePete O'Hanlon29-Jul-17 3:23 
GeneralRe: C# WeakClone and DeepClone Pin
Jeong, WonYoung(Brad)29-Jul-17 12:34
professionalJeong, WonYoung(Brad)29-Jul-17 12:34 
GeneralRe: C# WeakClone and DeepClone Pin
BillWoodruff29-Jul-17 6:18
professionalBillWoodruff29-Jul-17 6:18 
When you clone using Memberwise Clone, it creates a "shallow copy:" any internal fields in the cloned object that you don't set explicitly retain their value: for a cloned generic List that value is a reference: change that reference, and you change its value in any object that has a reference to it.

To make your code work: initialize the List in the MyClass constructor; and, in the Clone method initialize the cloned reference List. This is a bit tricky, and I suggest you spend some time carefully studying the examples here: [^]
public class MyClass : ICloneable
{
    public string Name { get; set; }

    public string Value { get; set; }

    public List<string> Lists; 

    public MyClass()
    {
        Lists = new List<string>() { "A", "B", "C" };
    }

    // you don't need this
    //public MyClass(MyClass c)
    //{
    //    Name = c.Name;
    //    Value = c.Value;
    //    Lists = c.Lists;
    //}

    public MyClass Clone()
    {
        var clone = this.MemberwiseClone() as MyClass;
        clone.Lists = new List<string>();
        return clone;
    }

    object ICloneable.Clone()
    {
        return this.MemberwiseClone();
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("Name={0}, ", Name);
        sb.AppendFormat("Value={0}, ", Value);
        sb.Append("Lists=");
        bool b = false;
        foreach (var item in Lists)
        {
            if (b)
                sb.AppendFormat("|{0}", item);
            else
                sb.AppendFormat("{0}", item); b = true;
        }
        return sb.ToString();
    }
}

«Differences between Big-Endians, who broke eggs at the larger end, and Little-Endians gave rise to six rebellions: one Emperor lost his life, another his crown. The Lilliputian religion says an egg should be broken on the convenient end, which is now interpreted by the Lilliputians as the smaller end. Big-Endians gained favor in Blefuscu.» J. Swift, 'Gulliver's Travels,' 1726CE

GeneralRe: C# WeakClone and DeepClone Pin
Jeong, WonYoung(Brad)29-Jul-17 12:36
professionalJeong, WonYoung(Brad)29-Jul-17 12:36 
GeneralRe: C# WeakClone and DeepClone Pin
BillWoodruff29-Jul-17 17:55
professionalBillWoodruff29-Jul-17 17:55 
QuestionIntermittent AccessViolationException with richtextbox Pin
shrishjain27-Jul-17 20:28
shrishjain27-Jul-17 20:28 
QuestionRe: Intermittent AccessViolationException with richtextbox Pin
Jochen Arndt27-Jul-17 23:01
professionalJochen Arndt27-Jul-17 23:01 
QuestionGet host name from different segment of LAN Pin
w1424327-Jul-17 17:39
w1424327-Jul-17 17:39 
AnswerRe: Get host name from different segment of LAN Pin
Jochen Arndt27-Jul-17 22:04
professionalJochen Arndt27-Jul-17 22:04 
AnswerRe: Get host name from different segment of LAN Pin
Nathan Minier28-Jul-17 1:19
professionalNathan Minier28-Jul-17 1:19 
AnswerRe: Get host name from different segment of LAN Pin
Member 1333447128-Jul-17 8:42
Member 1333447128-Jul-17 8:42 
QuestionCOM and C#.net Interoperability Pin
Vijjuuu.26-Jul-17 8:59
Vijjuuu.26-Jul-17 8:59 
AnswerRe: COM and C#.net Interoperability Pin
Richard MacCutchan26-Jul-17 10:05
mveRichard MacCutchan26-Jul-17 10:05 
AnswerRe: COM and C#.net Interoperability Pin
Bernhard Hiller26-Jul-17 22:05
Bernhard Hiller26-Jul-17 22:05 
GeneralRe: COM and C#.net Interoperability Pin
Vijjuuu.27-Jul-17 7:27
Vijjuuu.27-Jul-17 7:27 
GeneralRe: COM and C#.net Interoperability Pin
Pete O'Hanlon27-Jul-17 20:46
mvePete O'Hanlon27-Jul-17 20:46 
AnswerRe: COM and C#.net Interoperability Pin
BenScharbach12-Aug-17 10:21
BenScharbach12-Aug-17 10:21 
QuestionIssue with the below code Pin
Paul McGann25-Jul-17 22:44
professionalPaul McGann25-Jul-17 22:44 
AnswerRe: Issue with the below code Pin
F-ES Sitecore25-Jul-17 23:00
professionalF-ES Sitecore25-Jul-17 23:00 
GeneralRe: Issue with the below code Pin
Paul McGann26-Jul-17 3:14
professionalPaul McGann26-Jul-17 3:14 

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.