Search code examples
.netsyntactic-sugarnamed-parameters

.Net Parameter Attribute to make named/optional Parameter visible only internally/private?


Out of curiosity, is there a way to write a method e.g. like this:

public static MyType Parse(string stringRepresentation, [Internal] bool throwException = true)
{
// parsing logic here that conditionally throws an exception or returns null ...
}

public static MyType TryParse(string stringRepresentation)
{
return this.Parse(stringRepresentation, true);
}

I want to cut down code redundancies internally, but remain compliant to e.g. BCL method signatures for (Try)Parse() but if the c# compiler could in this case generate a second, internal method that would be nice.

Is that already somehow possible? Couldn't find anything so far.


Solution

  • I'm not aware that you can, but wouldn't this give you the same result?

    public MyType Parse(string stringRepresentation)
    {
        return this.Parse(stringRepresentation, true);
    }
    
    internal MyType Parse(string stringRepresentation, bool throwException = true)
    {
        // parsing logic here that conditionally throws an exception or returns null ...
    }