1
 Dictionary<string, int> test = new Dictionary<string, int>(); test.Add("dave", 12); test.Add("john", 14); int v; test.TryGetValue("dave", out int v) { Console.WriteLine(v); } 

This simple C# code gives "Best Overload Method Match has some Invalid Arguments" error. Can you please tell me the source of the error? Thanks.

8
  • What version of the compiler are you using? VS2017 likes that code just fine, once I fix the syntax error. Please post complete, valid example code that reliably reproduces the issue in some specific, named version of the compiler. Commented Oct 12, 2017 at 20:21
  • 3
    out int value is C# 7 only Commented Oct 12, 2017 at 20:22
  • 3
    Are you missing an if statement around test.TryGetValue? Commented Oct 12, 2017 at 20:22
  • @haim770 That error will be "invalid expression term int", not anything about the overload. Commented Oct 12, 2017 at 20:23
  • I am using Visual Studio 2012. I still receive the error despite the edits mentioned below. Thank you. Commented Oct 12, 2017 at 20:27

4 Answers 4

3

OP is in VS2012, not using C#7.

First, get rid of int in the parameter list. It can't be there in your version of C#.

Second, put a semicolon after the TryGetValue() call...

int v; test.TryGetValue("dave", out v); Console.WriteLine(v); 

Or put it in an if:

int v; if (test.TryGetValue("dave", out v)) { Console.WriteLine(v); } 
Sign up to request clarification or add additional context in comments.

Comments

0

Is "value" a variable you already declared or did you leave the example from the intellisense for TryGetValue? Pretty sure it's the latter case. edit: OR it's a new version of C# feature... This writes out 12 for v:

 Dictionary<string, int> test = new Dictionary<string, int>(); test.Add("dave", 12); test.Add("john", 14); int v; test.TryGetValue("dave", out v); { Console.WriteLine(v); } 

3 Comments

As others pointed out, your version uses this: blogs.msdn.microsoft.com/dotnet/2016/08/24/…
Here's a fiddle demonstrating it with the Roslyn compiler: dotnetfiddle.net/eblkGk
I never knew there was a dotnet fiddle. Thanks!
0

You got either a typo or a misunderstanding of

TryGetValue() 

There is no need for the code block where your writeline sits in. Just end your line of code and do your writeLine.

test.TryGetValue("dave", out int value); // <---- notice the ; Console.WriteLine(value); 

EDIT: Or, as Mr.Nimelo suggests, there might be an if statement missing like so:

if test.TryGetValue("dave", out int value) { Console.WriteLine(value); } 

Comments

0

Don't you miss an if in your snippet, nope?

 Dictionary<string, int> test = new Dictionary<string, int>(); test.Add("dave", 12); test.Add("john", 14); // missing if there? test.TryGetValue("dave", out int value) { Console.WriteLine(value); } 

My cheap 2 cents with a bit of refactoring...:

 var test = new Dictionary<string, int> {{"dave", 12}, {"john", 14}}; if (test.TryGetValue("dave", out var value)) { Console.WriteLine(value); } Console.ReadKey(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.