.NET(C#)中,当使用new Dictionary<TKey, TValue>()初始化一个字典时,可以通过集合初始化器语法直接为字典添加初始键值对。如需要为字典设置默认值,通常是指为尚未在字典中明确设置的键提供一个默认返回值。Dictionary<TKey, TValue> 类本身不直接支持默认值的概念,但可以通过扩展方法或在尝试访问字典时显式检查键的存在来模拟这种行为。

参考文档:.NET(C#)中new Dictionary(字典)初始化值(initializer默认值)-CJavaPy

1、C# 4.0中指定默认值的方法

在 C# 4.0 中,可以使用集合初始化器语法来初始化 Dictionary<TKey, TValue> 对象。集合初始化器语法可以使用大括号括起来的一系列键值对来初始化字典。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            //C# 4.0
            Dictionary<string, string> myDict =
                new Dictionary<string, string> { { "key1", "value1" },
                    { "key2", "value2" },
                    { "key3", "value3" }
                };
            Console.WriteLine(myDict);
            Dictionary<string, List<string>> myDictList =
                new Dictionary<string, List<string>> { { "key1", new List<string>            () { "value1" } },
                    { "key2", new List<string> () { "value2" } },
                    { "key3", new List<string> () { "value3" } }
                };
            Console.WriteLine(myDictList);
            Dictionary<int, StudentName> students = new Dictionary<int, StudentName>()
               {
                   { 111, new StudentName {FirstName="Sachin", LastName       ="Karnik",     ID=211}},
                   { 112, new StudentName {FirstName="Dina", LastName           ="Salimzianova", ID=317}},
                   { 113, new StudentName {FirstName="Andy", LastName="Ruth",        ID    =198}}
               };
            Console.WriteLine(students);
        }
    }
    class StudentName
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int ID { get; set; }
    }

}

2、C# 6.0中指定默认值的方法

在C# 6.0及之后的版本中,字典初始化可以更加简洁地通过索引器语法进行,这使得在声明并初始化一个Dictionary时代码更加简洁明了。这种方法不仅适用于字典,还适用于其他支持索引器的集合类型。这种初始化方式特别适合在需要预填充一些键值对到字典中的场景。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            Dictionary<int, StudentName> students = new Dictionary<int, StudentName>()
            {
                [111] = new StudentName { FirstName = "Sachin", LastName = "Karnik", ID = 211 },
                [112] = new StudentName { FirstName = "Dina", LastName = "Salimzianova", ID = 317 },
                [113] = new StudentName { FirstName = "Andy", LastName = "Ruth", ID = 198 }
            };
            Console.WriteLine(students);
            //C# 6.0
            Dictionary<string, string> dic =
                new Dictionary<string, string>
                {
                    ["key1"] = "value1",
                    ["key2"] = "value2",
                    ["key3"] = "value3"
                };
            Console.WriteLine(dic);
            Dictionary<string, List<string>> dicList =
                new Dictionary<string, List<string>>
                {
                    ["key1"] = new List<string>() { "value1" },
                    ["key2"] = new List<string>() { "value2" },
                    ["key3"] = new List<string>() { "value3" }
                };
            Console.WriteLine(dicList);
        }
    }
    class StudentName
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int ID { get; set; }
    }

}

参考文档:.NET(C#)中new Dictionary(字典)初始化值(initializer默认值)-CJavaPy

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐