汎用 OrderedDictionary の実装は特に難しいことではありませんが、不必要に時間がかかり、率直に言ってこのクラスは Microsoft 側の大きな見落としです。これを実装するには複数の方法がありますが、私は内部ストレージに KeyedCollection を使用することを選択しました。また、List
インターフェイスは次のとおりです。これには、Microsoft によって提供されたこのインターフェイスの非汎用バージョンである System.Collections.Specialized.IOrderedDictionary が含まれていることに注意してください。
// https://choosealicense.com/licenses/unlicense
// または https://choosealicense.com/licenses/mit/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace mattmc3.Common.Collections.Generic {
public interface IOrderedDictionary<tkey tvalue> : IDictionary<tkey tvalue>, IOrderedDictionary { new TValue this[int index] { get; set; } new TValue this[TKey key] { get; set; } new int Count { get; } new ICollection<tkey> Keys { get; } new ICollection<tvalue> Values { get; } new void Add(TKey key, TValue value); new void Clear(); void Insert(int index, TKey key, TValue value); int IndexOf(TKey key); bool ContainsValue(TValue value); bool ContainsValue(TValue value, IEqualityComparer<tvalue> comparer); new bool ContainsKey(TKey key); new IEnumerator<keyvaluepair tvalue>> GetEnumerator(); new bool Remove(TKey key); new void RemoveAt(int index); new bool TryGetValue(TKey key, out TValue value); TValue GetValue(TKey key); void SetValue(TKey key, TValue value); KeyValuePair<tkey tvalue> GetItem(int index); void SetItem(int index, TValue value); } </tkey></keyvaluepair></tvalue></tvalue></tkey></tkey></tkey>
}
ヘルパーを含む実装は次のとおりですクラス:
// http://unlicense.org
System を使用;
System.Collections.ObjectModel を使用;
System.Diagnostics を使用;
System.Collections を使用;
System.Collections.Specialized を使用;
System.Collections.Generic を使用;
を使用System.Linq;
名前空間 mattmc3.Common.Collections.Generic {
/// <summary> /// A dictionary object that allows rapid hash lookups using keys, but also /// maintains the key insertion order so that values can be retrieved by /// key index. /// </summary> public class OrderedDictionary<tkey tvalue> : IOrderedDictionary<tkey tvalue> { #region Fields/Properties private KeyedCollection2<tkey keyvaluepair tvalue>> _keyedCollection; /// <summary> /// Gets or sets the value associated with the specified key. /// </summary> /// <param name='"key"'>The key associated with the value to get or set. public TValue this[TKey key] { get { return GetValue(key); } set { SetValue(key, value); } } /// <summary> /// Gets or sets the value at the specified index. /// </summary> /// <param name='"index"'>The index of the value to get or set. public TValue this[int index] { get { return GetItem(index).Value; } set { SetItem(index, value); } } public int Count { get { return _keyedCollection.Count; } } public ICollection<tkey> Keys { get { return _keyedCollection.Select(x => x.Key).ToList(); } } public ICollection<tvalue> Values { get { return _keyedCollection.Select(x => x.Value).ToList(); } } public IEqualityComparer<tkey> Comparer { get; private set; } #endregion #region Constructors public OrderedDictionary() { Initialize(); } public OrderedDictionary(IEqualityComparer<tkey> comparer) { Initialize(comparer); } public OrderedDictionary(IOrderedDictionary<tkey tvalue> dictionary) { Initialize(); foreach (KeyValuePair<tkey tvalue> pair in dictionary) { _keyedCollection.Add(pair); } } public OrderedDictionary(IOrderedDictionary<tkey tvalue> dictionary, IEqualityComparer<tkey> comparer) { Initialize(comparer); foreach (KeyValuePair<tkey tvalue> pair in dictionary) { _keyedCollection.Add(pair); } } #endregion #region Methods private void Initialize(IEqualityComparer<tkey> comparer = null) { this.Comparer = comparer; if (comparer != null) { _keyedCollection = new KeyedCollection2<tkey keyvaluepair tvalue>>(x => x.Key, comparer); } else { _keyedCollection = new KeyedCollection2<tkey keyvaluepair tvalue>>(x => x.Key); } } public void Add(TKey key, TValue value) { _keyedCollection.Add(new KeyValuePair<tkey tvalue>(key, value)); } public void Clear() { _keyedCollection.Clear(); } public void Insert(int index, TKey key, TValue value) { _keyedCollection.Insert(index, new KeyValuePair<tkey tvalue>(key, value)); } public int IndexOf(TKey key) { if (_keyedCollection.Contains(key)) { return _keyedCollection.IndexOf(_keyedCollection[key]); } else { return -1; } } public bool ContainsValue(TValue value) { return this.Values.Contains(value); } public bool ContainsValue(TValue value, IEqualityComparer<tvalue> comparer) { return this.Values.Contains(value, comparer); } public bool ContainsKey(TKey key) { return _keyedCollection.Contains(key); } public KeyValuePair<tkey tvalue> GetItem(int index) { if (index = _keyedCollection.Count) { throw new ArgumentException(String.Format("The index was outside the bounds of the dictionary: {0}", index)); } return _keyedCollection[index]; } /// <summary> /// Sets the value at the index specified. /// </summary> /// <param name='"index"'>The index of the value desired /// <param name='"value"'>The value to set /// <exception cref='"ArgumentOutOfRangeException"'> /// Thrown when the index specified does not refer to a KeyValuePair in this object /// </exception> public void SetItem(int index, TValue value) { if (index = _keyedCollection.Count) { throw new ArgumentException("The index is outside the bounds of the dictionary: {0}".FormatWith(index)); } var kvp = new KeyValuePair<tkey tvalue>(_keyedCollection[index].Key, value); _keyedCollection[index] = kvp; } public IEnumerator<keyvaluepair tvalue>> GetEnumerator() { return _keyedCollection.GetEnumerator(); } public bool Remove(TKey key) { return _keyedCollection.Remove(key); } public void RemoveAt(int index</keyvaluepair></tkey></tkey></tvalue></tkey></tkey></tkey></tkey></tkey></tkey></tkey></tkey></tkey></tkey></tkey></tkey></tvalue></tkey></tkey></tkey></tkey>
以上がC# で汎用 OrderedDictionary を実装する理由の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

CプロジェクトにXMLを統合することは、次の手順を通じて達成できます。1)PUGIXMLまたはTinyXMLライブラリを使用してXMLファイルを解析および生成すること、2)解析のためのDOMまたはSAXメソッドを選択、3)ネストされたノードとマルチレベルのプロパティを処理する、4)デバッグ技術と最高の慣行を使用してパフォーマンスを最適化します。

XMLは、特に構成ファイル、データストレージ、ネットワーク通信でデータを構成するための便利な方法を提供するため、Cで使用されます。 1)tinyxml、pugixml、rapidxmlなどの適切なライブラリを選択し、プロジェクトのニーズに従って決定します。 2)XML解析と生成の2つの方法を理解する:DOMは頻繁にアクセスと変更に適しており、SAXは大規模なファイルまたはストリーミングデータに適しています。 3)パフォーマンスを最適化する場合、TinyXMLは小さなファイルに適しています。PugixMLはメモリと速度でうまく機能し、RapidXMLは大きなファイルの処理に優れています。

C#とCの主な違いは、メモリ管理、多型の実装、パフォーマンスの最適化です。 1)C#はゴミコレクターを使用してメモリを自動的に管理し、Cは手動で管理する必要があります。 2)C#は、インターフェイスと仮想方法を介して多型を実現し、Cは仮想関数と純粋な仮想関数を使用します。 3)C#のパフォーマンスの最適化は、構造と並列プログラミングに依存しますが、Cはインライン関数とマルチスレッドを通じて実装されます。

DOMおよびSAXメソッドを使用して、CのXMLデータを解析できます。1)DOMのXMLをメモリに解析することは、小さなファイルに適していますが、多くのメモリを占有する可能性があります。 2)サックス解析はイベント駆動型であり、大きなファイルに適していますが、ランダムにアクセスすることはできません。適切な方法を選択してコードを最適化すると、効率が向上する可能性があります。

Cは、高性能と柔軟性のため、ゲーム開発、組み込みシステム、金融取引、科学的コンピューティングの分野で広く使用されています。 1)ゲーム開発では、Cは効率的なグラフィックレンダリングとリアルタイムコンピューティングに使用されます。 2)組み込みシステムでは、Cのメモリ管理とハードウェア制御機能が最初の選択肢になります。 3)金融取引の分野では、Cの高性能はリアルタイムコンピューティングのニーズを満たしています。 4)科学的コンピューティングでは、Cの効率的なアルゴリズムの実装とデータ処理機能が完全に反映されています。

Cは死んでいませんが、多くの重要な領域で栄えています。1)ゲーム開発、2)システムプログラミング、3)高性能コンピューティング、4)ブラウザとネットワークアプリケーション、Cは依然として主流の選択であり、その強力な活力とアプリケーションのシナリオを示しています。

C#とCの主な違いは、構文、メモリ管理、パフォーマンスです。1)C#構文は最新であり、LambdaとLinqをサポートし、CはC機能を保持し、テンプレートをサポートします。 2)C#はメモリを自動的に管理し、Cは手動で管理する必要があります。 3)CパフォーマンスはC#よりも優れていますが、C#パフォーマンスも最適化されています。

tinyxml、pugixml、またはlibxml2ライブラリを使用して、CでXMLデータを処理できます。1)XMLファイルを解析する:DOMまたはSAXメソッドを使用し、DOMは小さなファイルに適しており、SAXは大きなファイルに適しています。 2)XMLファイルを生成:データ構造をXML形式に変換し、ファイルに書き込みます。これらの手順を通じて、XMLデータを効果的に管理および操作できます。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター
