Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

How to use struct with List in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public struct Test {
    public int n;
    private List<string> lst;

    public Test(int _n) {
        n = _n;
        lst = new List<string>();
    }

    public void AddWord(string s) => lst.Add(s);

    public override string ToString() => $"{n} [{string.Join(", ", lst)}]";
}

public class Program
{
    public static void Main()
    {
        var s1 = new Test(888);
        
        s1.AddWord("c#");
        Console.WriteLine("s1 - " + s1);  

        var s2 = s1;
        s2.n = 999;
        s2.AddWord("c++");

        Console.WriteLine("s1 - " + s1);  
        Console.WriteLine("s2 - " + s2);  
    }
}



/*
run:

s1 - 888 [c#]
s1 - 888 [c#, c++]
s2 - 999 [c#, c++]

*/

 



answered Nov 22, 2020 by avibootz

Related questions

1 answer 92 views
1 answer 145 views
145 views asked Dec 15, 2020 by avibootz
1 answer 121 views
1 answer 126 views
126 views asked Dec 15, 2020 by avibootz
1 answer 150 views
2 answers 168 views
168 views asked Jan 18, 2017 by avibootz
1 answer 94 views
94 views asked Nov 22, 2020 by avibootz
...