Set array key as string not int?

前端 未结 6 1040
耶瑟儿~
耶瑟儿~ 2020-12-03 06:58

I am trying to set the array keys as a strings like in the example below, but inC#.



        
6条回答
  •  眼角桃花
    2020-12-03 07:19

    You can also use a KeyedCollection http://msdn.microsoft.com/en-us/library/ms132438%28v=vs.110%29.aspx where your value is a complex type and has a unique property.

    You have your collection inherit from KeyedCollection, example ...

        public class BlendStates : KeyedCollection
        {
        ...
    

    This requires you to override the GetKeyForItem method.

        protected override string GetKeyForItem(BlendState item)
        {
            return item.DebugName;
        }
    

    Then, in this example, the collection is indexed by string (the debug name of the BlendState):

        OutputMerger.BlendState = BlendStates["Transparent"];
    

提交回复
热议问题