Perchik
Perchik

Reputation: 5620

Mystical "F colon" in c#

I was working on refactoring some c# code with ReSharper. One of the things I've run into is a c# operator that I'm unfamiliar with.

In my code, I had this

Mathf.FloorToInt(NumRows/2)

where NumRows is an integer. ReSharper suggests that I should change it to

Mathf.FloorToInt(f: NumRows/2)

I'm pretty sure that f: is some flag that tells it to cast NumRows as a float but I cannot find any documentation for f: online. Can anyone elaborate on what exactly f: does or link me to a MSDN page about it?

(Although I have a good idea of what f: does, it's hard to search the internet for a colon, and I'd like to know what it does before I use it)

Update 1: Regardless of what I'm trying to do, I'm interested in the f-colon syntax

Update 2: Turns out it was actually Visual Studio suggesting that I could add the argument name 'f' not ReSharper, but that does't change the correct answer..

Upvotes: 12

Views: 770

Answers (3)

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

It's a named parameter. Look at the defintion of Mathf.FloorToInt, it will have a parameter named f.

Resharper is indicating that the code could be made more readable by using a named parameter in this case.

Upvotes: 21

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

In the C# 4.0, you can switch around the parameter expressions in method invocation.

When there is only one parameter, it does not help you much, if at all: there is no doubt about what the expression represents, if there is only one parameter. However, with multiple parameters, the feature becomes a lot more helpful: you can pair up parameter names with expressions representing their values, and pass parameters in any order that you like. Readers of your program would not need to refer to the method signature in order to understand what expression represents which parameter.

private static void MyMethod(int a, int b, int c) {
    Console.WriteLine("{0} {1} {2}", a, b, c);
}

public static void Main(string[] args) {
    MyMethod(1, 2, 3);
    MyMethod(c:1, a:2, b:3);
}

This prints

1 2 3
2 3 1

Upvotes: 9

Hans Z
Hans Z

Reputation: 4744

You are looking at the named parameter syntax.

Upvotes: 3

Related Questions