18 uncommon C # keywords

Source: Internet
Author: User
Tags new set

1. _ arglist

Let's start with _ arglist. _ Arglist is used to send parameters to methods. We usually pass parameters to the method through the parameter list specified in the function header. If we want to pass a new set of parameters to the method, we need to overload the method. If the number of parameters we want to pass is unknown, we need to use the param array. Why do we need to use _ arglist? The following problems exist in the above two methods:

A. If we use method overloading, we need to add a new function overload once a set of new parameters are passed.

B. if we use the param array, the parameter type must be the same or the param object array should be used.

_ Arglist can solve the above problem. You can pass any parameter to the function. It can be of any type. We can analyze each parameter using simple steps.

Let's take a look at the following code:

1 public int paramLength (_ arglist)
2 {
3 ArgIterator iterator = new ArgIterator (_ arglist );
4 return iterator. GetRemainingCount ();
5}

If I use the following code to call this function

1 int x = this. paramLength (_ arglist (49,34, 54,6, "Manimoy"); // returns 5

5 is returned to variable x. This is because five parameters are passed to this method. We can access each parameter, such:

1 TypedReference tf = iterator. GetNextArg ();
2 TypedReference. ToObject (tf)

Each time GetNextArg is called, GetRemainingCount is reduced by 1 until every object in the iterator is traversed.

2. _ refvalue 

Another interesting keyword is _ refvalue. It is used to obtain the value of a referenced object. You can obtain the actual object from the TypedReference object. This requires two parameters. The first one is the TypedReference object and the other is the conversion type. Take a look at the following code:

1 int tfValue = _ refvalue (tf, int );

3. _ makeref

_ Makeref identifies the TypedReference object from the object. This is just the opposite of _ refvalue. See the following code:

1 string name = "Ayan ";
2 TypedReference tf = _ makeref (name );

4. _ reftype

_ Reftype is used to obtain the type from objects of the TypedReference type. See the following code:

1 Type t = _ reftype (tf );
2 if (t. ToString (). equals ("System. String "))
3 string str = _ refvalue (t, string );

Note: Although I found these keywords in all C # versions, I didn't use them at work. In future versions of C #, these keywords may not exist, so whether to use them depends on your own risks.

5. Yield

Yield appears in. in. NET 2.0, Yield is returned in the form of enumerated object values. The yield statement can only appear in the iterator block. This block can be used as the body of methods, operators, or accessors. In the following code, I constructed a name list and returned a list of names with a length less than 5. The yield break statement is returned.

1 List <string> lst = new List <string> ();
2 lst. Add ("Abhishek ");

Related Article

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.