5 Replies - 434 Views - Last Post: 07 August 2012 - 02:27 AM Rate Topic: -----

#1 goodsir  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 25-July 12

string vs list in method use

Posted 06 August 2012 - 06:58 PM

I was bored and just fiddling around, when I wrote something that wasn't working for me:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            string s1 = string.Empty;

            input = Console.In.ReadLine();
            RemoveDup(input);
            Sort(input);
            Console.Out.WriteLine(input);
            Console.ReadKey();

        }
        static void RemoveDup(string str)
        {
            string finalString = string.Empty;
            int len = str.Length;
            for (int i = 0; i < len; i++)
            {
                string toAdd = str.Substring(i, 1);
                for (int j = i + 1; j < len; j++)
                {
                    if (toAdd.Equals(str.Substring(j,1)))
                    {
                        toAdd = "";
                        break;
                    }
                }
                finalString += toAdd;
            }
            str = finalString;
        }
        static void Sort(string str)
        {
            char[] toSort = str.ToCharArray();
            str = string.Empty;

            bool doneSearching = false;

            while (!doneSearching)
            {
                doneSearching = true;
                for (int i = 0; i < toSort.Length - 1; i++)
                {
                    if (toSort[i] < toSort[i + 1])
                    {
                        char temp = toSort[i];
                        toSort[i] = toSort[i + 1];
                        toSort[i + 1] = temp;
                        doneSearching = false;
                        break;
                    }
                }
            }
            foreach (char toAdd in toSort)
            {
                str += toAdd;
            }
        }
    }
}


this program is supposed to remove the duplicates of a string and then write it sorted. However this does not work, so my guess is that my methods create a copy of the arguments being passed to it. To make the program work, I made them return string values rather than void. But what confused me is that when I'm working with something like lists, and write a simple program like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {

            List<int> list = new List<int>();
            list.Add(0);
            Console.Out.WriteLine("BEFORE CHANGE:");
            foreach(int num in list)
            {
                Console.Out.WriteLine(num);
            }
            AddToList(list);
            Console.Out.WriteLine("AFTER CHANGE:");
            foreach (int num in list)
            {
                Console.Out.WriteLine(num);
            }
            Console.ReadKey();
        }
        static void AddToList(List<int> list)
        {
            for (int i = 1; i <= 20; i++)
            {
                list.Add(i);
            }
        }
    }
}

does the list's changes stay, making me think that my theory that methods make copies of their arguments to be incorrect. Can someone please help explain to me what's going on?

Is This A Good Question/Topic? 0
  • +

Replies To: string vs list in method use

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: string vs list in method use

Posted 06 August 2012 - 07:17 PM

Strings are value types in C#. Most other objects in C# are reference types. When you assign a value type to variable or pass it in as a parameter, a copy is made. Any manipulations you do only apply to that copy. When you assign a reference type to a variable or pass it in a parameter, a reference is made. Any manipulations you do affect the object.
Was This Post Helpful? 2
  • +
  • -

#3 goodsir  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 25-July 12

Re: string vs list in method use

Posted 06 August 2012 - 07:34 PM

Thanks a lot for that. So, take my Sort method for instance... Would a more "correct" version return a string? ex:
static string Beautify(string str)
        {
            string toReturn = string.Empty;
            char[] toSort = str.ToCharArray();

            bool doneSearching = false;

            while (!doneSearching)
            {
                doneSearching = true;
                for (int i = 0; i < toSort.Length - 1; i++)
                {
                    if (toSort[i] < toSort[i + 1])
                    {
                        char temp = toSort[i];
                        toSort[i] = toSort[i + 1];
                        toSort[i + 1] = temp;
                        doneSearching = false;
                        break;
                    }
                }
            }
            foreach (char toAdd in toSort)
            {
                toReturn += toAdd;
            }
            return toReturn;
        }
or would I be better off using a reference type?
Was This Post Helpful? 0
  • +
  • -

#4 goodsir  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 25-July 12

Re: string vs list in method use

Posted 06 August 2012 - 07:42 PM

View Postgoodsir, on 06 August 2012 - 07:34 PM, said:

Thanks a lot for that. So, take my Sort method for instance... Would a more "correct" version return a string? ex:
static string Beautify(string str)
        {
            string toReturn = string.Empty;
            char[] toSort = str.ToCharArray();

            bool doneSearching = false;

            while (!doneSearching)
            {
                doneSearching = true;
                for (int i = 0; i < toSort.Length - 1; i++)
                {
                    if (toSort[i] < toSort[i + 1])
                    {
                        char temp = toSort[i];
                        toSort[i] = toSort[i + 1];
                        toSort[i + 1] = temp;
                        doneSearching = false;
                        break;
                    }
                }
            }
            foreach (char toAdd in toSort)
            {
                toReturn += toAdd;
            }
            return toReturn;
        }
or would I be better off using a reference type?

:oops:
Sorry for posting before further reading. For those that, like me, haven't a good grasp of the subject, this may be useful: http://www.albahari....vsreftypes.aspx... After reading that, I realized I could simply use the "ref" keyword. Again, thanks a lot for the help.
Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: string vs list in method use

Posted 07 August 2012 - 12:21 AM

View PostSkydiver, on 07 August 2012 - 02:17 AM, said:

Strings are value types in C#. Most other objects in C# are reference types. When you assign a value type to variable or pass it in as a parameter, a copy is made.


Actually, strings are reference types in C#, and behave as such with regards to assignment and parameter passing, i.e. the reference is assigned/passed by value.

This post has been edited by CodingSup3rnatur@l-360: 07 August 2012 - 12:25 AM

Was This Post Helpful? 4
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: string vs list in method use

Posted 07 August 2012 - 02:27 AM

Yup, CodingSup3rnatur@l-360, is correct. I stand corrected.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1