Tyler Pantuso
Tyler Pantuso

Reputation: 853

Can I use the keyword "in" to separate parameters in a method declaration somehow?

I would like to create a method that uses the keyword in instead of a comma to separate parameters in a method declaration; something similar to the foreach(a in b) method.

Example

Class Structure

public class Length
{
    public double Inches;
    public double Feet;
    public double Yards;

    public enum Unit { Inch, Foot, Yard }

    Dictionary<Unit, double> inchFactor = new Dictionary<Unit, double>()
    {
        { Unit.Inch, 1 },
        { Unit.Foot, 12 },
        { Unit.Yard, 36 }
    };

    public Length(double value, Unit unit)
    {
        this.Inches = value * inchFactor[unit];
        this.Feet = this.Inches / inchFactor[Unit.Foot];
        this.Yards = this.Inches / inchFactor[Unit.Yard];
    }
}

Method Definition in Class

// I'd like to know how to use "in" like this  ↓
public List<Length> MultiplesOf(Length divisor in Length dividend)
{
    double inchEnumeration = divisor.Inches;
    List<Length> multiples = new List<Length>();

    while (inchEnumeration <= dividend.Inches)
    {
        multiples.Add(new Length(inchEnumeration, Length.Unit.Inch));
        inchEnumeration += divisor.Inches;
    }

    return multiples;
}

Ideal Implementation

private void DrawRuler()
{
    Length eighthInch = new Length(0.125, Length.Unit.Inch);
    Length oneFoot = new Length(1, Length.Unit.Foot);

    // Awesome.
    List<Length> tickGroup = Length.MultiplesOf(eighthInch in oneFoot);

    double inchPixels = 10;
    foreach (Length tick in tickGroup)
    {
        // Draw ruler.
    }
}

I've looked into creating new keywords, but it looks like C# does not support defining keywords.

Upvotes: 1

Views: 62

Answers (2)

Xiaoy312
Xiaoy312

Reputation: 14477

While you can't redefine an existing keyword, there is other way to accomplish what you in a slightly different way using Fluent Interface :

public class Length
{
    // ...

    public static IFluentSyntaxProvider MultiplesOf(Length divisor)
    {
        return new FluentSyntaxProvider(divisor);
    }

    public interface IFluentSyntaxProvider
    {
        List<Length> In(Length dividend);
    }
    private class FluentSyntaxProvider : IFluentSyntaxProvider
    {
        private Length divisor;

        public FluentSyntaxProvider(Length divisor)
        {
            this.divisor = divisor;
        }

        public List<Length> In(Length dividend)
        {
            double inchEnumeration = divisor.Inches;
            List<Length> multiples = new List<Length>();

            while (inchEnumeration <= dividend.Inches)
            {
                multiples.Add(new Length(inchEnumeration, Length.Unit.Inch));
                inchEnumeration += divisor.Inches;
            }

            return multiples;
        }
    }
}

Example of usage :

// Awesome.
List<Length> tickGroup = Length.MultiplesOf(eighthInch).In(oneFoot);

Upvotes: 1

Douglas
Douglas

Reputation: 54917

As has been mentioned in the comments, you cannot define custom keywords in C# (unless you extend the compiler, which is an advanced task). However, if your goal is to clarify the meaning of the two arguments, then I would suggest using named arguments instead:

// Define the method as usual:
public List<Length> MultiplesOf(Length divisor, Length dividend)
{
    // ...
}

// Then call it like so, explicitly showing what is the divisor and the dividend:  
List<Length> tickGroup = Length.MultiplesOf(divisor: eighthInch, dividend: oneFoot);

Upvotes: 2

Related Questions