C # reference context keywords: get, set, value, partial, where, and yield (zhuan)

Source: Internet
Author: User

The context keyword is used to provide specific meanings in the code, but it is not a reserved word in C.

Get, set, value

GetDefine the "accessor" method in the attribute or indexer to retrieve the value of this attribute or the indexer element.

SetThe "accessor" method in the attribute or indexer, used to set the value of the attribute or indexer element.

ValueImplicit parameters are used to set accessors and add or remove event handlers.

// Simple example
Class Employee
{
Private string _ name;
Public string Name
{
Get {return this. _ name ;}
Set {this. _ name = value ;}
}
}

Note::

  1. Get and set "accessors", which have the same access level by default. However, given the read/write considerations, the access level of the set can be restricted. Access modifiers for attributes or indexers are subject to the following conditions:
    1. You cannot use accessors for interfaces or explicit interface members.

    2. Accessors can be used only when the property or indexer has both set and get accessors. In this case, only the modifier can be used for one of the accessors.

    3. If the attribute or index appliance has an override modifier, The accesser modifier must match the accesser of the rewritten accesser (if any.

    4. The Accessibility level of the accesser must be more restrictive than the Accessibility level of the attribute or the indexer itself.

Partial

PartialThe Division type definition allows you to split a class, structure, or interface definition into multiple files.

[Modifiers] partial type

  1. Modifiers is optional. It can be abstract, new, override, static, virtual, extern, and an access modifier.
  2. Type can be one of the classes, structures, and interfaces.

Example:

The following categories will be merged during compilation, including their methods and Type features.

 1 namespace Hunts.Keywords 2 { 3     [System.Serializable] 4     partial class Test 5     { 6         void Test1() { } 7     } 8     [Conditional("DEBUG")] 9     partial class Test10     {11         void Test2() { }12     }13 }

Class equivalent:

 1 namespace Hunts.Keywords 2 { 3     [System.Serializable] 4     [Conditional("DEBUG")] 5     class Test 6     { 7         void Test1() { } 8         void Test2() { } 9     }10 }

For detailed usage of some (classes, interfaces, and structures), see the partial classification in the MSDN Library.

Where

WhereThe clause is used to specify type constraints. These constraints can be used as variables for type parameters defined in generic declarations.

  1. Type constraints are used because if you want to check an item in the generic list to determine whether it is valid or compare it with another item, the compiler must ensure that the operators or methods to be called by the compiler are supported by any type of parameters that may be specified by the client code. This guarantee is obtained by applying one or more constraints to the generic class definition.
// Syntax: public class MyGenericClass <T> where T: something
    1. Something can be a structure, class, new (), <Base Class Name>, or <Interface Name>.
    2. You can have multiple constraints in 1 at the same time, and the constraints themselves can also be generic.
    3. You can also apply constraints to generic methods or delegation.

For more information, see "Generic programming" and "Type parameter constraints" in the MSDN Library.

Example:

 1 // keywords_where.cs 2  3 using System; 4 using System.Collections; 5  6 struct MyStruct 7 { 8     //... 9 }10 11 interface IMyInterface12 {13     //...14 }15 16 class MyGenericClass<T1,T2>17     where T1: IEnumerable, IMyInterface18     where T2: MyStruct, new()19 {20     public void MyMethod(T1 t1, T2 t2)21     {22         //...23     }24 }
Yield

YieldThe iterator block is used to provide a value to the enumerated number object or send an iteration end signal.

// Expression is calculated and returned in the form of enumerated object values. Expression must be implicitly converted to the yield type of the iterator. Yield return expression; yield break;

Example:

1 // keywords_yield.cs 2 using System; 3 using System. collections; 4 5 namespace Hunts. keywords 6 {7 public class Employee 8 {9 private string _ name; 10 private int _ id; 11 12 public string Name13 {14 get {return this. _ name;} 15 set {this. _ name = value;} 16} 17 18 public int ID19 {20 get {return this. _ id;} 21 set {this. _ id = value;} 22} 23 24 // number 25 public static IEnumerable SetIDs (string [] names) 26 {27 int counter = 0 for the person in the given array; 28 Employee result = new Employee (); 29 while (counter ++ <names. length) 30 {31 result. _ id = counter; 32 result. _ name = names [counter-1]; 33 yield return result; 34} 35} 36} 37 38 class EmployeeList39 {40 static void Main () 41 {42 string [] names = {"Jones", "Carl", "Dennis", "Evan", "Hugo", "Ivan "}; 43 44 // display the result of the number operation 45 foreach (Employee e in Employee. setIDs (names) 46 {47 Console. writeLine ("ID: {0} Name: {1}", e. ID, e. name); 48} 49 50 Console. read (); 51} 52} 53}

Note::

  1. The yield statement can only appear in the iterator block. This block can be used as a method, operator, or accessors. These methods, operators, or accessors are subject to the following constraints:

    • Insecure blocks are not allowed.
    • The parameters of methods, operators, or accessors cannot be ref or out.
  2. Yield statements cannot appear in anonymous methods.
  3. When used together with expression, the yield return statement cannot appear in catch blocks or try blocks that contain one or more catch clauses.

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.