8
Answers

ContainsKey

Maha

Maha

11y
1.4k
1
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)