Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c# why do I get the same output using classes?

Ive written code that sorts the given input but after it returns the sorted input it will always return the same output. I’m creating console applications using .NET 5.0 (current) in Visual Studio.

When I give as input "Car Apple Banana" it get sorted in words.Sorted()

After that I print out the original input but it seems to be sorted too. I don’t know why because I never sort it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When the input is: "Car Apple Banana"

The output I now get is:

Apple Banana Car

Apple Banana Car

While it needs to be:

Apple Banana Car

Car Apple Banana

Here’s the main code:

using System;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;

namespace _10_Words
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input_1 = Console.ReadLine().Split(' ');
            Words words = new Words(input_1);

            Console.WriteLine(words.Sorted());
            Console.WriteLine(words.Normal());
        }
    }
}

Here’s the class code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10_Words
{
    class Words
    {
        public string[] Output { get; set; }

        public Words(string[] input)
        {
            Output = input;
        }

        public string Sorted()
        {
            string[] sorted = Output;

            Array.Sort(sorted);

            string sorted_array = string.Join(" ", sorted);
            return Convert.ToString(sorted_array);
        }

        public string Normal()
        {
            string[] normal = Output;

            string normal_output = string.Join(" ", normal);
            return Convert.ToString(normal_output);
        }
    }
}

>Solution :

It seems that you assume that the line

string[] sorted = Output;

copies input to Output, whereupon youc an sort Output while leaving input unchanged.

This is not the case.

Arrays are reference types. As such, when you assign an array like Output to another array variable such as sorted, sorted and Output will reference the same array.

If you want a copy, you will have to iterate over the elements or use a helper method, such as Array.Copy:

sorted = new string[Output.Length];
Array.Copy(Output, sorted, Output.Length);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading