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.

40,023 questions

51,974 answers

573 users

How to copy string in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string s1 = "a new day";
                string s2 = string.Copy(s1);

                Console.WriteLine(s1); // a new day
                Console.WriteLine(s2); // a new day

                Console.WriteLine(object.ReferenceEquals(s1, s2)); // False
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

a new day
a new day
False

*/


answered Mar 21, 2015 by avibootz
edited Mar 21, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string s1 = "a new day";
                string s2 = s1;

                Console.WriteLine(s1); // a new day
                Console.WriteLine(s2); // a new day

                // s1 and s2 point to the same place in memeory

                Console.WriteLine(object.ReferenceEquals(s1, s2)); // True
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

a new day
a new day
True

*/


answered Mar 21, 2015 by avibootz
edited Mar 21, 2015 by avibootz

Related questions

1 answer 154 views
1 answer 134 views
1 answer 125 views
3 answers 274 views
274 views asked Mar 22, 2015 by avibootz
1 answer 254 views
1 answer 84 views
84 views asked Aug 12, 2023 by avibootz
...