Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# params keyword with two parameters of the same type

I just encountered something with C# today that I hadn't thought of before. I have two methods in my class, one an overload of the other. They are declared like so:

  1. public void RequirePermissions(params string[] permissions)...
    
  2. public void RequirePermissions(string message, params string[] permissions)...
    

In my code, I tried to call the first one like so:

RequirePermissions("Permission1", "Permission2");

...expecting it to call the first overload. Well it called the second overload. The only way I can get it to call the first method in this case is to manually pass a string[] object like so:

RequirePermissions(new string[] { "Permission1", "Permission2" });

Now, this behavior doesn't confuse me because I understand that the compiler can't tell which method I actually wanted to call based on my provided parameters. But was I not careful this could have gone unnoticed in my code. It seems as though Microsoft should have made the compiler throw an error when it encountered a situation like above. Does anyone have any thoughts on this? Is there another way to call the first overload other than the "solution" I posted?

like image 810
Andy Avatar asked Sep 25 '09 20:09

Andy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


2 Answers

Agreeing with Adam, I'd change it to something like:

public void RequirePermissions(params string[] permissions)

public void RequirePermissionsWithMessage(string message, params string[] permissions)
like image 169
scottm Avatar answered Nov 15 '22 21:11

scottm


Personally, I'd do it this way:

  1. public void RequirePermissions(params string[] permissions)...
    
  2. public void RequireMessageAndPermissions(string message, 
        params string[] permissions)...
    

People fall too in love with overloading sometimes, and when you combine that with a love for the params keyword, you just increase the confusion level for whomever eventually has to take over your code.

like image 38
MusiGenesis Avatar answered Nov 15 '22 21:11

MusiGenesis