Forcing named arguments in C# - c#

C# 4 introduced a feature called named arguments which is especially useful in scenarios like
int RegisterUser(string nameFirst, string nameLast, string nameMiddle, string email)
Is there a way to force using named arguments? Maybe some attribute to apply to a method or a compiler switch I'm not aware of? I guess it can be done with code inspector tools but just want to know if there is other way.
p.s.
For those interested why one may need it and why not just use a class/struct to utilize object initializers there are scenarios when it's impossible. Like calls to libraries not in your control or weird code conventions you have to obey.

It's possible to force the callers to always use named args. I wouldn't do this in most circumstances because it's rather ugly, but it depends on how badly safe method usage is needed.
Here is the solution:
int RegisterUser(
#if DEBUG
int _ = 0,
#endif
string nameFirst = null,
string nameLast = null,
string nameMiddle = null,
string email = null) { /*...*/ }
The first parameter is a dummy that shouldn't be used (and is compiled away in Release for efficiency). However, it ensures that all following parameters have to be named.
Valid usage is any combination of the named parameters:
RegisterUser();
RegisterUser(nameFirst: "Joe");
RegisterUser(nameFirst: "Joe", nameLast: "Smith");
RegisterUser(email: "joe.smith#example.com");
When attempting to use positional parameters, the code won't compile.

No, not in the C# language. It will always accept positional parameters if all the parameters are supplied.
You could build a custom FxCop rule or an StyleCop rule to enforce this - as pointed out in the comments, it is likely a StyleCop rule you would be interested in (thanks to Kris).

Sorry for a shameless plug!
I implemented a Roslyn analyzer to enforce using named arguments for a method.
So if you install the RequireNamedArgs analyzer and add a special comment before a method that should be invoked with named arguments:
//[RequireNamedArgs]
int RegisterUser(string nameFirst, string nameLast, string nameMiddle, string email)
The analyzer will emit an error if a caller attempts to use positional arguments instead of named.
Take a look at it in action:
If you decide to give it a go -- do so at your own risk :)

I've also sought a way to force named arguments. Optional parameters can be dangerous, especially if you have multiple parameters of the same type. Overloads are almost always a safer solution, but there are times when you have a method that can take many combination of arguments, so creating 20 overloads to account for ever possibility is overkill.
In extreme situations where it is of the utmost importance that arguments be named at all times, I will create an argument class with no defined constructor. In your case, you could do this:
public class UserRegistrationArguments
{
public string nameFirst { get; set; }
public string nameLast { get; set; }
public string nameMiddle { get; set; }
public string email { get; set; }
}
Call it like this:
RegisterUser(new UserRegistrationArguments { nameFirst = "Bob", nameLast = "Slob" });
You could also simplify it like this:
public class UserRegistrationArguments
{
public string nameMiddle { get; set; }
public string email { get; set; }
}
int RegisterUser(string nameFirst, string nameLast, UserRegistrationArguments args = null)
...and do this:
RegisterUser("Bob", "Slob", new UserRegistrationArguments { nameMiddle = "Teh" });
This way, you only have one optional parameter and that's for your optional parameters.
Edit: Maybe I didn't read the OP correctly. You're not using optional arguments? If not then this answer probably doesn't help you.

I'm using another method. In my setup I have 1 parameter which I always expect, then come a bunch of optional strings which I really want to be sure the user chose actively. So my first string in this list is a "trap" value, which if set, throws an error. Like this:
public HtmlString Toolbar(DynamicEntity target = null, string dontRelyOnParameterOrder = Constants.RandomProtectionParameter, string actions = null, string contentType = null, object prefill = null)
{
if (!Enabled) return null;
protectAgainstMissingParameterNames(dontRelyOnParameterOrder);
var toolbar = new ItemToolbar(target, actions, contentType, prefill);
return new HtmlString(toolbar.Toolbar);
}
private void protectAgainstMissingParameterNames(string criticalParameter)
{
if(criticalParameter != Constants.RandomProtectionParameter)
throw new Exception("when using the toolbar command, please use named parameters - otherwise you are relying on the parameter order staying the same.");
}
Hope you like it :)

Related

How to add optional parameters to C#-Method

I have a method which searches for employees. This method got a lot of optional parameters as search-parameters. My Api is used in many single programs in our system and i want to add two new optional parameter to it. If iam doing so compiler is fine, but the using programs of my api are getting Method missing exceptions. Ok i understand that so far, because intern the old Method isnt longer existing (parameterlist is different). Now i thought i easily can overload it. But now the compiler can't different my two methods (old and overload) for sure.
Small example:
Old-Version:
public virtual List<IEmployee> Search(int? personalNr = null, bool? active = null, DateTime? dateOfBirth = null)
Needed-Version:
public virtual List<IEmployee> Search(int? personalNr = null, bool? active = null, DateTime? dateOfBirth = null, string firstName = null, string lastName = null)
Just want to add two parameters. I know i can compile all using dll, but this API is used massive and i don't want to transfer all dll's on Live-System.
Is there a common way how to handle this situation?
CLARIFY
1.
I just add two new optional parameters to the existing method to extend it:
All calling programs getting Missing-Method Exception because the signature changed.
2.
I overload the method. Now the compiler can't differ the overload and the method. This i quite clear to me. Someone could call Search(active: true); which method should .Net take?
From my point of view the best way to do it is :
declare a private method that need all the parameters and which manage the emptyness (or not) of each parameters.
declare one public method for each number of parameters possible
each public method call the private method converting missing parameters to empty parameters.
From my point of view i don't think using optionnal paremeters is a good idea
Hey thank you guys for response. My problem is that i don't want to effect the programs outside. So i decided to create a new class SearchParams. This holds all parameters listed. So i can overload my existing method and pass SearchParams as parameter. I will set old method Obsolete. The SearchParams class is free extendable then.
Changes:
Create new class like this:
public class SearchParams
{
public int? PersonalNr{get;set;}
public bool? Active {get;set;}
public DateTime? DateOfBirth{get;set}
public string FirstName{get;set;}
public string LastName{get;set;}
}
Overload Search-Method:
public List<IEmployee> Search(SearchParams paramList)
So the caller first creates the parameter and pass this to the search.
In my opinion this seems to be the best way.
This is another suggestion.
You can remove optional parameters from the old method. Make all parameters mandatory.
public virtual List<object> Search(int? personalNr, bool? active, DateTime? dateOfBirth)
{
}
Then make all parameters optional in your second method.
public virtual List<object> Search(int? personalNr = null, bool? active = null, DateTime? dateOfBirth = null, string firstName = null, string lastName = null)
{
}
Now suppose you are calling this method like this;
Search(1,true, DateTime.Now);
The above will execute your old method.
Search(1,true, DateTime.Now, null);
This will execute your new method.
However, if I was in your position, I would have just renamed the old method.
The problem may come from the position of your optional parameters.
It seems that it is compiling "for some reason" but that should not be working.
As a workaround, you should modify your needed version :
public virtual List<IEmployee> Search(int? personalNr = null, bool? active = null, DateTime? dateOfBirth, string firstName = null, string lastName = null)
Leave the old method as well but move the code from your "basic" version to the overloaded one. And when the "basic" search is called, just call your search method and fit the firstName and the lastName as null parameters.
EDIT: Just saw your edit, my post does not make much sense then :)

C# method parameter being a property of the class

First of all, apologies if I posted it in the wrong place, I'm new here and I'm not sure if I posted in the right place.
Well, I'm trying to build a generic search method, where I'll add search parameters to mount a SQL Query and execute it on the database. All that using C#. My goal is that the parameter corresponding to the field I'll search, to be a property of the class the method is in. For example:
public foo
{
public string CustomerCode { get; set; }
public string CustomerName { get; set; }
public void AddSearchParameter(???, EnumOperator Operator, object Value)
}
Whenever I want to specify a parameter to add on the search, I would like it to look like this:
foo xxx = new foo();
xxx.AddSearchParameter(foo.CustomerCode, EnumOperator.Equal, txtCustomerCode.text);
My question is how to do it?
If you are trying to pass the member information (so that the AddSearchParameter can inspect the MemberInfo and write suitable SQL), then you'd need to use either a string literal (i.e. "CustomerCode"), or an expression tree. The latter is richer, but involves learning the Expression<T> API. But fundamentally:
public void AddSearchParameter(Expression<Func<object>> expression, ...)
...
xxx.AddSearchParameter(() => foo.CustomerCode, ...)
This, however, is not a trivial area of .NET.
If I were doing something like this, I would probably make the Search() method on foo check for the existence of values in the various this properties, and then build the query based on that.
public List<Results> Search()
{
if (!String.IsNullOrEmpty(this.CustomerCode))
{
// add search value to query
}
// etc.
}

How to define named Parameters C#

This seems like a simple question, but for some reason I can't find the answer anywhere. Basically, I'd like to be able to implement a constructor that takes NamedParameters.
By named parameters, I do not mean parameters with default values (optional parameters) such as:
public SomeMethod(){
string newBar = Foo(bar2 : "customBar2");
}
public string Foo(string bar1 = "bar1", bar2 = "bar2" ){
//...
}
A good example of what I'm trying to achieve is the AuthorizeAttribute from the System.Web.Mvc assembly. Which you can use the following way:
[Authorize(Roles = "Administrators", Users = "ThatCoolGuy")]
public ActionResult Admin(){
}
The constructor's signature in intellisense looks like the following example and I believe (please confirm) that those NamedParameters are mapping to class properties.
AuthorizeAttribute.AuthorizeAttribute(NamedParameters...)
Initiliaze new instance of the System.Web.Mvc.AuthorizeAttribute class
Named parameters:
Order int
Users string
Roles string
Please note:
The syntax of defining the parameter name when calling a method has nothing to do with optional parameters:
You can use Foo(bar1 : "customBar1"); even if Foo is declared like this: void Foo(string bar1)
To answer the question:
My guess is that this is syntactic sugar similar to the object initializers introduced in Visual Studio 2010 and therefore you can't use this for your own classes.
The behaviour you are talking about is specific for attributes and cannot be reused in "normal" classes constructors.
You don't need to "implement" anything.
The parameters can be used in the manner you describe just by existing as parameters on the constructor.
You do need to be using C# 3.5 or above, when they were introduced.
Your example will compile and run on C# 4.0 / Visual Studio 2010 without modification.
See Named and Optional Arguments (C# Programming Guide) on MSDN.
In regards to properties on the object, that do not have a corresponding constructor arguments, the exact syntax is specific to attributes and can't be replicated, though, with object initializers you can get close.
You can use the builder/constructor info pattern together with property initializers.
class PersonInfo
{
public string Name { get; set; }
public int? Age { get; set; }
public Color? FavoriteColor { get; set; }
public Person BuildPerson()
{
return new Person(this);
}
}
class Person
{
public Person(PersonInfo info)
{
// use info and handle optional/nullable parameters to initialize person
}
...
}
var p = new Person(new PersonInfo { Name = "Peter", Age = 15 });
// yet better
var p = new PersonInfo { Name = "Peter", Age = 15 }.BuildPerson();
I however don't understand, why you don't just use named parameters and provide null for indicating optional parameters.
class Person
{
public Person(string name = null, int? age = null, Color? favoriteColor = null) { /* ... */ }
}
var p = new Person(name: "Peter", age: 15);
Named parameters are NOT specific to attributes. It's a language syntax that can be used everywhere. It's fine to use properties for initialisers but you don't always want to have internals set as set properties.
Just instantiate you class using:
TheClass c = new Theclass(param3:firstValue, param1:secondValue, param2:secondValue);
With regards to this part of the question:
"I however don't understand, why you don't just use named parameters and provide null for indicating optional parameters."
The reason named parameters are nice is you don't need to provide extraneous values in parentheses, just what you want to specify, because if it's optional you shouldn't even need to put null. Furthermore, if you specify null, you are overriding any default value for that parameter which makes it optional. Being optional implies there's a default value meaning nothing passed in.
Property initialisation at instance time is purely there for convenience. Since C there has been the ability to initialise values at construction time on types. Which is handy if those values can't be specified in the constructor. I personally feel that the convenience of them has spoiled people and it get a little too liberal and make everything public get AND set. Just depends on the design and security of properties you need.
I doubt that's possible. This is something specific for attributes.
I think the closest option is to use an object initializer:
class Foo {
public string Name {get;set;}
public int Data {get;set;}
}
var foo = new Foo {Name = "MyName", Data = 12};
try to use this signature
[AttributeUsage(AttributeTargets.Class)]
before the name of your class
Please refer to MSDN specification for full description:
https://msdn.microsoft.com/en-us/library/aa664614(v=vs.71).aspx
"Each non-static public read-write field and property for an attribute class defines a named parameter for the attribute class".
Visual C# 2010 introduces named and optional arguments. Named argument able you to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list.Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods.
static void Main(string[] args)
{
mapingFunction(snum2: "www.stackoverflow.com", num1: 1);
}
public static void mapingFunction(int num1, string snum2)
{
Console.WriteLine(num1 + " and " + snum2);
}
here you can see that argument are passed with our their order
What you probably want to do is implement public properties in your attribute:
public class AuditFilterAttribute : ActionFilterAttribute
{
public string Message { get; set; }
public AuditFilterAttribute() { }
}
They can be accessed through Named Parameters where you apply it:
[AuditFilter(Message = "Deleting user")]
public ActionResult DeleteUser(int userId)
Hope that helps...

Need to understand overloaded constructors

I study C# and I'm trying to understand the overloaded constructor, how it works and the point using them like a chain call? Why just not have only one constructor with all necessary parameters? Below I have some helping code for a task I'm working with, and I need some help to understand the point with all this constructors. Preciate some help! Thanks!
public class Email
{
//private email
private string m_personal;
//work mail
private string m_work;
public Email()
{
}
public Email(string workMail) : this(workMail, string.Empty)
{
}
public Email(string workMail, string personalMail)
{
m_work = workMail;
m_personal = personalMail;
}
public string Personal
{
//private mail
get { return m_personal; }
set { m_personal = value; }
}
public string Work
{
get { return m_work; }
set { m_work = value; }
}
public string GetToStringItemsHeadings
{
get { return string.Format("{0,-20} {1, -20}", "Office Email", "Private Email"); }
}
public override string ToString()
{
string strOut = string.Format("{0,-20} {1, -20}", m_work, m_personal);
return strOut;
}
}
Why just not have only one constructor with all necessary parameters?
What if users of your class are only interested in some parameters? Using your example, what if somebody doesn't have personal email? Should they pass null or string.Empty? This kind of type-level knowledge is best handled by type itself.
By exposing extra constructor with work email only, you're essentially telling your class consumers "Don't have personal email? Don't worry, I will handle it".
On top of that, chaining definitions naturally helps to avoid code redundancy.
This is not about Constructors. All kind of overloaded functions simplify library usage. When we code a class, we are going to encapsulate all the complexities in a black box and this is possible with some known best Practices. The good example is .NET libraries which you can use easily (remember those overloaded functions/constructors).
This is not about right or wrong, we use overloaded functions to make usage simpler so there would be no need to pass null parameters when it is not needed.
Secondly we call the function with most parameters nested by the next function with less parameters to reduce Redundancy. I mean avoiding copy/paste same code in all versions of function.
I think since the pattern is widely accepted and used, next generations of .NET will hide this redundant versions in some way and we just code the function with the most parameters.
The point is to avoid repeated code, as it will help you avoid this:
public Email(string workMail)
{
m_work = workMail;
m_personal = string.Empty;
}
public Email(string workMail, string personalMail)
{
m_work = workMail;
m_personal = personalMail;
}
Take in account that your constructor could do more than just assigning fields.
How it works? You can try it, debug it and learn it. I'm not sure but I think it will call first the overloaded one and then the code of your constructor. In you scenario, calling Email(string workMail) will call first Email(string workMail, string personalMail).
Check out:
http://www.c-sharpcorner.com/UploadFile/vishnuprasad2005/HowuseCSharpConstructors11302005015338AM/HowuseCSharpConstructors.aspx
http://blog.degree.no/2012/03/calling-an-overloaded-constructor-from-another-constructor-c/
http://weblogs.asp.net/scottcate/archive/2005/11/23/431412.aspx
I would say that the second constructor is provided mainly for convenience purposes, to make the usage of the class easier in cases where there is no personal mail address. The user of the class then only needs to specify the working address, and the class itself will take care of the handling of the nonexistend personal address by setting a sensible default value for that field. If that parameter was missing, the user of the class would be made resposible for filling string.Empty for missing personal mail addresses.
The constructor is implemented as a call to another constructor to avoid duplication of code. It could also be written like
public Email(string workMail)
{
m_work = workMail;
m_personal = string.Empty;
}
but the existing implementation that calls the two-parameter constructor is the cleaner solution because it follows the "Don't repeat yourself" principle.

Help me understand NewExpression.Members

Does NewExpression.Members inform the LINQ runtime how to map a type's constructor parameters to its properties? And if so, is there an attribute to set the mapping? I'm imagining something like this:
public class Customer
{
public Customer(int id, string name)
{
Id = id;
Name = name;
}
[CtorParam("id")]
public int Id { get; set; }
[CtorParam("name")]
public string Name { get; set; }
}
But none of the MSDN docs really inform you how exactly Members is initialized.
My limited understanding is that you don't usually need to pass the member information; the arguments are taken (by position) from the arguments parameter. The member info is (I suspect) intended to help some internal APIs when dealing with things like anonymous-types, which look (in C#) like they are initialized by member (like an object-initializer), but which are actually initialized by constructor. This means things like LINQ-to-SQL will see a constcutor use, and then (in the next part of the query) access to obj.Name - it needs a way to understand that this means "the 3rd parameter to the constructor (which never actually gets called). In particular for things like groupings.
So this is fine:
var param = Expression.Parameter(typeof(string), "name");
var body = Expression.New(typeof(Customer).GetConstructor(new[] {typeof(int), typeof(string)}),
Expression.Constant(1), param);
var func = Expression.Lambda<Func<string, Customer>>(body, param).Compile();
var cust = func("abc");
If you do need them, I would expect them to be positional relative to the "arguments" expressions - so you would pass in (in an array) the member for id and name. Note that there is also a separate expression for intialzer-style binding.

Categories