Jump to content

C# params keyword - couple of questions

kaddle

i started learning about the params keyword recently. i get the gist of how it works. i just have some questions so that i can get additional information. i'm very new to programming just to let you know. simple and detailed explanations will be easier for me to understand. i typed out the code below from a template in the book i'm learning from. the disposable array and the strings passed when calling the method don't have a significant meaning.

 

        static void Main(string[] args)
        {
            void NamePrinter(params string[] tasks)
            {
                for (int i = 0; i < tasks.Length; i++)
                {
                    Console.Write(tasks[i] + ", ");
                }
                Console.WriteLine();
            }
            NamePrinter("Jon", "Jan", "Kim", "Bak", "Hok", "Rip");
            Console.ReadLine();

the way this method works has similarities to the code in last thread that i made, so i pretty much understand the way it works. i only have one question relating to the way that the method works.

 

my first question is, is the params keyword only used in front of an array? like are there other ways that the params keyword is used? if so, any examples will be helpful.

 

regarding the for loop variable 'i'. i know that this variable represents the element positions in the 'tasks' array. my question is does the value of 'i' increase depending on how many strings are passed when calling the method?

for example if i put < NamePrinter("Bob", "Bil", "Ben"); > will the value of 'i' be 2 to represent each name that i put into the array?

Link to comment
Share on other sites

Link to post
Share on other sites

Yes params is only for arrays. It allow for unlimited parameters of that type listed one behind each other as INDIVIDUAL parameter.

 

The difference between what i guess is confusing you is between :

void NamePrinter(params string[] tasks)

void NamePrinter(string[] tasks)

 

For the code inside the method it is the exact same. But one catch is that you can have only 1 params in the parameter of a method and it has to be the last parameters if you have many parameters.

 

For calling the method this is where it differs visually for the developper. the first one the compiler will accept either a single array OR (the reason why you use params) many comma separated values of the array type required. You will mainly use params in console applications to allow many parameters without specific order.

 

The second method need to pass and array which you need to declare and fill before passing it.

 

You won't see often params in standard methods but i do use it from time to time as it make sense for some methods to use it. Compiling is the same so it's only for code readability in method (other than console main). I would say i might open couple project with millions of methods and find 2 or 3 using params that are not the main console input.

 

As for does i increase yes it is. the length will increase. But you should favor this format if you want to loop on all items all the time :

void NamePrinter(params string[] tasks)
{
    foreach(string task in tasks)
    {
        Console.Write(task + ", ");
    }
    
    Console.WriteLine();
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×