views:

88

answers:

2

Today I had something weird happen in my copy of Resharper 5. I have a class that looks like this:

public class Foo
{
    public string Username { get; private set; }

    public Foo (string userName) { Username = userName; }

    public void Bar()
    {
        DoWork(Username);
    }

    public void DoWork(string userName) { }
}

When I start to type DoWork(us I get the following from intellisense:

alt text

Notice that it's pulling up the constructor argument, and it ends with a colon: userName:

What's going on here?

EDIT:

As Reed answered below, this is a new C# 4 feature called Named and Optional Arguments. It's purpose is to allow you to specify the name of an argument, rather than it's position in a parameter list. so you don't have to remember the position of an argument in the argument list to use it (though this is largely meaningless with intellisense). It does make optional arguments easier to use though.

Thanks Reed.

+2  A: 

You didn't include the ; at the end of userName

public Foo (string userName) { Username = userName; }
joshlrogers
I didn't read close enough. I was assuming because the lack of ; it was still seeing itself in the Foo method for intellisense purposes but because of the : at the end of userName it is for a named parameter as Reed said.
joshlrogers
This is true, but not what is causing the confusion here. He is also missing a return type for the DoWork method, and (in his original) would have had it defined as "public void DoWork(string userName)" [with a capitalized N in userName]...
Reed Copsey
Fixed the return type.. While trying to simplify the example, typos are a killer.. thanks for pointing them out.
Mystere Man
+9  A: 

This is Resharper providing intellisense support for Named and Optional Arugments.

C# 4 added support for these. You can now have a method defined like this:

public void DoWork(int someArgument = 3, string userName = "default") 
{
    // ...

If you want to call this with a different "userName" but leave the default for other parameters, you can do:

this.DoWork(userName: "FooUser");

Resharper 5 adds support for this syntax in intellisense.

Reed Copsey
This is the correct answer, I didn't read close enough I guess.
joshlrogers
It didn't help that I made two typos (the semicolon and not capitalizing the parameter of DoWork.. fixed now). I suspected this might be some esoteric new C# 4 feature, but wasn't sure. I thought it was the constructor parameter when in actuality it was the DoWork() parameter.
Mystere Man

related questions