C # basic keyvaluepair usage and generic usage

Source: Internet
Author: User

C # keyvaluepair <tkey, tvalue> usage. Struct, which defines key/value pairs that can be set or retrieved. That is to say, we can use it to record a key/value pair. For example, if we want to define a key/value pair such as ID (INT type) and name (string type), this can be used.

 

/// <Summary>
/// Set the key/value pair
/// </Summary>
/// <Returns> </returns>
Private keyvaluepair <int, string> setkeyvaluepair ()
{
Int intkey = 1;
String strvalue = "my value ";
Keyvaluepair <int, string> kVp = new keyvaluepair <int, string> (intkey, strvalue );
Return kVp;
}

/// <Summary>
/// Obtain the key/value pair
/// </Summary>
Private void getkeyvaluepairdemo ()
{
Keyvaluepair <int, string> kVp = setkeyvaluepair ();
Int intkey = kVp. Key;
String strvalue = kVp. value;
}

 

This is also the case if you want to use generics. Generally, when you only need to read two fields (ID and name) in batches, use keyvaluepair with generics, for example:

1. read data from the database

 

/// <Summary>
/// Obtain the ID (enterprise_id) and English name (enterprise_name_eng) of all enterprises)
/// </Summary>
/// <Returns> All enterprise IDs and English names in the enterprise_info table </returns>
Public list <keyvaluepair <long, string> getenterpriseidandnameengist ()
{
// Enterprise_id and enterprise_name_eng Pairs
List <keyvaluepair <long, string> lstidkeynameengvalue = new list <keyvaluepair <long, string> ();

String plain text = "select incluise_id, incluise_name_eng from incluise_info ";
Using (oracledatareader reader = oraclehelper. executereader (oraclehelper. oracleconnstring, commandtype. Text, plain text, null ))

{
Try
{
Myeventlog. log. debug ("plain text =" + plain text );
While (reader. Read ())
{
Keyvaluepair <long, string> idkeynameengvalue = new keyvaluepair <long, string> (
Reader. isdbnull (0 )? 0: reader. getint64 (0 ),
Reader. isdbnull (1 )? String. Empty: reader. getstring (1)
);
Lstidkeynameengvalue. Add (idkeynameengvalue );
}
Oraclehelper. datareaderclose (Reader );
}
Catch (oracleexception E)
{
Myeventlog. log. Error ("plain text =" + plain text );
Myeventlog. log. Error (E );
Throw E;
}
}
Return lstidkeynameengvalue;
}

 

2. process data in the business

 

/// <Summary>
/// Function functions:
/// 1. Return the valid enterprise Id set from the name of the enterprise to be imported.
/// 2. Return a valid enterprise row number set.
/// 3. Invalid enterprise row number set is returned.
/// </Summary>
/// <Param name = "lstenterprisenameen"> set of enterprise names to be imported </param>
/// <Param name = "lstvalidrowsindex"> valid enterprise ID row set in the Excel table </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)
{
// Valid enterprise ID line
Lstvalidrowsindex = new list <int> ();
// Invalid enterprise ID line
Lstinvalidrowsindex = new list <int> (); // obtain all enterprise IDs and English names
List <keyvaluepair <long, string> lstidkeynameengvalue = Dal. getenterpriseidandnameenglist (); // used to store valid enterprise IDs. If the English name of the enterprise can be found in the enterprise_info table, this indicates that the enterprise exists, therefore, the existing enterprise ID is obtained and stored in this variable.
List <long> lstvalidenterpriseid = new list <long> (); // a list of valid enterprise IDS can be obtained through the following loop:
For (INT I = 0; I <lstenterprisenameen. Count; I ++)
{
Foreach (keyvaluepair <long, string> kVp in lstidkeynameengvalue)
{
If (lstenterprisenameen [I] = kVp. value)
{
// Obtain a valid row Index
Lstvalidrowsindex. Add (I); // obtain a valid enterprise ID
Lstvalidenterpriseid. Add (kVp. Key); // immediately jumps out of the inner loop after a valid ID is found and returns to the outer loop
Continue;
}
} If (! Lstvalidrowsindex. Contains (I )&&! Lstinvalidrowsindex. Contains (I ))
{
// Obtain an invalid row Index
Lstinvalidrowsindex. Add (I );
}
}
Return lstvalidenterpriseid;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.