Question

I am creating C# custom attribute that I wan to pass some parameters. I have 4 parameters and one of them is optional. As I found, optional parameters has to be set as named parameters, while required ones are position parameters.

What if I write code in such fashion that I want all parameters to be used as named (looks cleaner to me)? Should required params always be positioned, and as that distinguished form optional? What is best practice on this?

Here is my code:

[AttributeUsage(AttributeTargets.Field)]
public class MyAttribute : Attribute
{
    private string _field1 = null;
    private string _field2 = null;
    private string _field3 = null;
    private string _field4 = null;

    public MyAttribute(string Field1, string Field2, string Field3)
    {
        _field1 = Field1;
        _field2 = Field2;
        _field3 = Field3;
    }

    public string Field4
    {
        get { return _field4; }
        set { _field4 = value; }
    }        
}
Was it helpful?

Solution

Optional parameters should be at the end of your parameter list (and any subsequent parameters after it should also be optional), e.g:

public MyAttribute(string Field1, string Field2, string Field3, string Field4 = "")
{
}

In that declaration, it is the last parameter that is optional and any parameters beyond it must also be optional. This is highlighted in the quote from MSDN:

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported. For example, in the following code, instance method ExampleMethod is defined with one required and two optional parameters.

OTHER TIPS

I wanted to comment first to clarify but my reputation didn't allow me to. :( haha

First, let me clarify what is your concept of named parameter? You mentioned "As I found, optional parameters has to be set as named parameters." Because if you are refer to the docs it doesn't. A named parameter is just for the caller of the method to specify which value is which without depending on how the order of the parameters in the method declaration. This is the usage of a named parameter:

public static void Main()
{
   Example e = new Example(height: 64, weight: 123);
}

public class Example
{
    int _weight;
    int _height;

    public CalculateBMI(int weight, int height)
    {
        _weight= weight;
        _height = height;
    }
}

The concept of Named parameters is just to allow you to say which is which.

Example e = new Example(height: 64, weight: 123);

Now your question of "What if I write code in such fashion that I want all parameters to be used as named (looks cleaner to me)?" You cannot force a method to have the caller call the method and specify the name of all parameters. That's just not the concept of Named parameters.

If I misunderstood you just let me know and I'll be glad to correct anything that I said or add details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top