The relationship between KeyValuePair and Dictionary
The relationship between KeyValuePair and Dictionary
1. KeyValuePair
a, KeyValuePair is a structure (struct);
b. KeyValuePair only contains a key-value pair of Key and Value.
2. Dictionary
a. Dictionary can be simply regarded as a collection of KeyValuePair ;
b. Dictionary can contain multiple Key and Value key-value pairs.
The following code may help to better understand the relationship between them:
Dictionary < int , string > myDic = new Dictionary < int , string >();
foreach ( KeyValuePair < int , string > item in myDic )
{
Console.WriteLine ( "Key = {0}, Value = {1}" , item.Key , item.Value );
}
The usage of C# KeyValuePair<TKey,TValue>. The structure defines the key/value pairs that can be set or retrieved. In other words, we can record a value such as a key/value pair through it. For example, if we want to define a key/value pair like ID (int type) and Name (string type), then we can use it like this.
/// <summary>
/// Set key/value pairs
/// </summary>
/// <returns></returns>
private KeyValuePair<int, string> SetKeyValuePair()
{
}
/// <summary>
/// Get key/value pairs
/// </summary>
private void GetKeyValuePairDemo()
{
}
If you want to use generics, the same is true.Generally, when reading data in batches, when you only need to read two fields (Id and Name), if you don’t want to use the Model class, and use KeyValuePair with generics, for example:
1.Read data from the database
/// <summary>
/// Get the Id (enterprise_id) and English name (enterprise_name_eng) of all companies
/// </summary>
/// <returns>All the company IDs and English names in the enterprise_info table</returns>
public List<KeyValuePair<long,string>> GetEnterpriseIdAndNameEn
{
}
2.Processing data in business
/// <summary>
/// Function:
/// 1.Return the set of valid company Id obtained from the company name to be imported.
/// 2.Return a valid enterprise line number set.
/// 3.Return an invalid enterprise line number set.
/// </summary>
/// <param name=”lstEnterpriseNameEn”>The enterprise name (English) set to be imported</param>
/// <param name=”lstValidRowsIndex”>Valid enterprise Id row in Excel Set</param>
/// <param name=”lstInvalidRowsIndex”>Invalid enterprise Id row set in the Excel table</param>
/// <returns>returns the index list of valid rows</returns>
public List<long > PrepareForImport(List<string> lstEnterpriseNameEn, out List<int> lstValidRowsIndex, out List<int> lstInvalidRowsIndex)
{
atorvastatin over the counter & lt;a href="https://lipiws.top/"& gt;buy lipitor without a preion& lt;/a& gt; lipitor for sale
Jfhgds
2024-03-09