Maha

Maha

  • NA
  • 0
  • 309.3k

ContainsKey

Oct 1 2013 2:32 PM
This program is given in the following website. Please explain why o/p is 1000, which is coming from Console.WriteLine(dictionary["mac"]);. Problem is highlighted.

http://www.dotnetperls.com/containskey

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// Create Dictionary with two key value pairs.
var dictionary = new Dictionary<string, int>(){{"mac", 1000}, {"windows", 500}};

// Use ContainsKey method.
if (dictionary.ContainsKey("mac") == true)
{
Console.WriteLine(dictionary["mac"]); // <-- Is executed
}

// Use ContainsKey method on another string.
if (dictionary.ContainsKey("acorn"))
{
Console.WriteLine(false); // <-- Not hit
}
Console.ReadKey();
}
}
//1000


Answers (8)