I have a function that counts the commas in each item of an array of string.

public int CommaCount(string[] sarray)
        {
            int count = 0;
            int commas = 0;
            for (int i = 0; i < sarray.Length; i++)
            {
                commas = sarray[i].Count(c => c == ',');
                if (commas > count)
                {
                    count = commas;
                }
            }
            return count;
        }

However I'd like for the function to not be able to modify the array, which is not const.

I would have thought the following might have done it, but I get intellisense errors with varying messages, the first of which is under the const word in function argument "Type Expected"

public int CommaCount(const string[] sarray)
        {
            int count = 0;
            int commas = 0;
            for (int i = 0; i < sarray.Length; i++)
            {
                commas = sarray[i].Count(c => c == ',');
                if (commas > count)
                {
                    count = commas;
                }
            }
            return count;
        }

Is there a way I can achieve such functionality?

Recommended Answers

All 10 Replies

You would essentially need a deep copy clone of the array. Array only has a shallow copy though, so you would need to find some code that allows for deep copying. This would cause overhead if you were doing it a lot though. Arrays are pass by reference to prevent the overhead of copying a whole array over and over again into local variables. Usually programmers make a concious effort not to modify the array or make changes in a method which should have no side effects. Essentially a deep copy would get you the same effect as pass by value. You should also know that arrays in C# "pass a reference by value". This is somewhat confusing because it is not what typical languages do. So basically if I wanted to change the contents of the array, that would propagate back to the calling method, but if I wanted to get rid of the reference entireley, setting the array to null, then the operation would not propagate back to the calling method because the reference is passed by value.

Here is a discussion where somebody brought up deep copying an array, I like the GenericCopier solution;
http://stackoverflow.com/questions/4054075/how-to-make-a-deep-copy-of-an-array-in-c

I hope nobody takes offence to me linking to stack overflow, I use you guys pretty much interchangably, I try not to limit myself when it comes to resources.

Thanks for your in depth reply.

Yes I am trying to avoid a copy operation, which as you mention is essentially the same as passing a variable by value.

I have just found a discussion wherin it seems to be established that this is just not possible and perhaps (it is mentioned) silly to even want to do it.

http://bytes.com/topic/c-sharp/answers/250452-why-not-const-ref-params-c

It's not something that I'm worried about though, as I'll just make sure the array is not modified. It was more curiosity as why what I thought would work, did not.

Thank you again for your time.

Hi Suzie999:
Just to clarify, do you mean an array of strings, for example:

string[] strArrays = { {this, is, one, set, of, strings},
                       {this, is, another, set, of, strings},
                       {this, is, a, third, set, of, strings}
                     };

Or did you mean a string array, for example

string[] strArray = {"The", "quick", "brown", "fox" };

If you're referring to the first example, you could use a nested loop, to first iterate through each array, counting all the commas as you go. If you're referring to the second example you can use a for loop and the substring() method (or use the query syntax of LINQ) to count each comma. Let me know if you have any questions.

Tekkno

You could do it like this

            string[] wibble = { "AAAA", "BBB,B", "CCCCC" };

            Console.WriteLine(CommaCount(wibble.ToArray()));

Or like this:

        public static int CommaCount(string[] sarray)
        {
            var copy = sarray.ToArray();

            int count = 0;
            int commas = 0;

            for (int i = 0; i < copy.Length; i++)
            {
                commas = copy[i].Count(c => c == ',');

                if (commas > count)
                {
                    count = commas;
                }
            }

            return count;
        }

In C# const has only one meaning as opposed to C++ where it is used more often in different places.
In C# you have const aType aName = aValue; and that's all there is. Btw. in C# a string is immutable, so you could say is "const" anyway.

Thanks guys.

To answer some questions, I'm already using LINQ Count method to count the commas, and a copy operation is too much overhead for me.

My array is constructed like so...

string fileread = File.ReadAllText(filename);
string[] lines = fileread.Split(new char[] { '\n' },
                StringSplitOptions.None);

Thanks again.

Sorry one last thing to add!

You can use File.ReadAllLines which already returns a string array so no need for lines 2 and 3.

That's something, thanks DaveAmour.

(edit) name spelling.

Suzie you got my name wrong :(

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.