C # method usage Summary (getenumerator usage ,?? Usage)

Source: Internet
Author: User

Directory:

1 ,?? Operator usage

2. getenumerator Method

3. The ResourceManager. getstring method obtains the characters of resources.

4. Get the characters of the settings file.

I ,?? It may be a forgotten operator, which is rarely used. Its usage is simple but practical:
Variable ?? Defaultvalue
Equivalent
Variable = NULL? Defaultvalue: Variable
With it, a row can handle lazy evaluation:
Use ?? Before:

Public useraccess users
{
Get
{
If (_ users = NULL)
{
_ Users = proxy. getqueryobject <useraccess> ();
}
Return _ users;
}
}

After:

Public useraccess users
{
Get
{
Return _ users ?? (_ Users = proxy. getqueryobject <useraccess> ());
}
}

Note: This operator only supports the reference type and nullable type.

Int? That is, nullable <int> is supported by the nullable type.

Original article: http://www.cnblogs.com/Dah/archive/2007/09/29/910479.html

 

2. getenumerator

The following example describes how to use the getenumerator method. This includes deleting the row enumeration number from the basic able when the number of enumerations is active.

View plaincopy to clipboardprint?
Public static void main ()
{
Try
{
Datatable usertable = new datatable ("peopletable ");

Usertable. Columns. Add ("ID", typeof (INT ));
Usertable. Columns. Add ("name", typeof (string ));

// Note that even if you create the datatablereader
// Before adding the rows, the enumerator can still
// Visit all the rows.
Datatablereader reader = usertable. createdatareader ();
Usertable. Rows. Add (new object [] {1, "Peter "});
Usertable. Rows. Add (new object [] {2, "Mary "});
Usertable. Rows. Add (new object [] {3, "Andy "});
Usertable. Rows. Add (new object [] {4, "Russ "});

Ienumerator enumerator = reader. getenumerator ();
// Keep track of whether the row to be deleted
// Has actually been deleted yet. This allows
// This sample to demonstrate that the enumerator
// Is able to keep ve row deletion.
Bool isrowdeleted = false;
While (enumerator. movenext ())
{
Dbdatarecord dataRecord = (dbdatarecord) enumerator. Current;

// While the enumerator is active, delete a row.
// This doesn' t affect the behavior of the enumerator.
If (! Isrowdeleted)
{
Isrowdeleted = true;
Usertable. Rows [2]. Delete ();
}
Console. writeline (dataRecord. getstring (1 ));
}
}
Catch (exception ex)
{
Console. writeline (Ex );
}
Console. Readline ();

Original article: http://www.cnblogs.com/suiqirui19872005/archive/2007/08/11/851752.html

2.2 Second usage

Const int times = 1000; public static void Test2 () {stopwatch watch = new stopwatch (); hashtable hastable = new hashtable (); For (INT I = 0; I <10000; I ++) {hastable. add (I, I. tostring () + "value");} // test getenumerator watch. start (); For (INT I = 0; I <times; I ++) {idictionaryenumerator enumerator = hastable. getenumerator (); While (enumerator. movenext () {string key = enumerator. key. tostring (); string value = enumerator. value. tostring () ;}} watch. stop (); console. writeline ("hashtable getenumerator time consumption" + watch. elapsedmilliseconds); console. writeline ("---------------"); watch. reset (); // test foreach watch. start (); For (INT I = 0; I <times; I ++) {foreach (Object item in hastable. keys) {string key = item. tostring (); string value = hastable [item]. tostring () ;}} watch. stop (); console. writeline ("hashtable foreach time consumed" + watch. elapsedmilliseconds); console. writeline ("---------------"); watch. reset (); dictionary <int, string> dictionary = new dictionary <int, string> (); For (INT I = 0; I <10000; I ++) {dictionary. add (I, I. tostring () + "value");} watch. start (); For (INT I = 0; I <times; I ++) {dictionary <int, string>. enumerator enumerator = dictionary. getenumerator (); While (enumerator. movenext () {int key = enumerator. current. key; string value = enumerator. current. value ;}} watch. stop (); console. writeline ("dictionary getenumerator time consumption" + watch. elapsedmilliseconds); console. writeline ("---------------"); watch. reset (); // test foreach watch. start (); For (INT I = 0; I <times; I ++) {foreach (INT item in dictionary. keys) {int key = item; string value = dictionary [item] ;}} watch. stop (); console. writeline ("dictionary foreach time consumed" + watch. elapsedmilliseconds); console. writeline ("---------------"); console. readkey ();}}

Original article: http://www.cnblogs.com/scottckt/archive/2011/05/16/2048243.html

3. Obtain the characters of resources. Use the ResourceManager. getstring method to obtain the string characters defined in properties. Resources. Wpfapplicationsomemethodtest. properties. Resources. ResourceManager. getstring ("teststring ");

4. Get the characters of the settings file. Wpfapplicationsomemethodtest. properties. settings. Default. defaultfolder;

 

 


 

 

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.