In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the keywords get, set, value, partial, where and yield in C#. It is very detailed and has a certain reference value. Interested friends must finish reading it!
C # keywords-get, set, value
Get defines an accessor method in a property or indexer to retrieve the value of the attribute or indexer element.
The accessor method in the set semantic property or indexer that sets the value of the property or indexer element.
Value implicit parameters that are used to set accessors and to add or remove event handlers.
/ / simple example class Employee {private string _ name; public string Name {get {return this._name;} set {this._name = value;}
Note:
Get, set "accessor", have the same access level by default. However, sometimes, given the consideration of reading and writing, it is usually possible to limit the access level of set. The use of access modifiers on properties or indexers is restricted by the following conditions:
You cannot use accessor modifiers on interfaces or explicit interface member implementations.
Accessor modifiers can be used only if a property or indexer has both set and get accessors. In this case, only one of the accessors is allowed to use modifiers.
If the property or indexer has an override modifier, the accessor modifier must match the accessor of the overridden accessor, if any.
The accessibility level of the accessor must be more restrictive than that of the property or indexer itself.
C # keyword-partial
Partial partial type definitions allow you to split the definition of a class, structure, or interface into multiple files.
[modifiers] partial type
Modifiers is optional. It can be one of abstract, new, override, static, virtual, extern, and access modifiers.
Type can be one of the classes, structures, and interfaces.
Example:
Some of the following classes will be merged at compile time, including its methods, type properties, and so on.
Namespace Hunts.Keywords {[System.Serializable] partial class Test {void Test1 () {}} [Conditional ("DEBUG")] partial class Test {void Test2 () {}
Class is equivalent to:
Namespace Hunts.Keywords {[System.Serializable] [Conditional ("DEBUG")] class Test {void Test1 () {} void Test2 () {}}
For detailed use of parts (classes, interfaces, structures), you can refer to some classes in MSDN Library.
C # keyword-where
The where clause is used to specify type constraints that can be used as variables for type parameters defined in a generic declaration.
Type constraints are used because if you want to check an item in a generic list to determine whether it is valid, or compare it to some other item, the compiler must guarantee to some extent that the operator or method it needs to call will be supported by any type of parameter that the client code may specify. This guarantee is achieved by applying one or more constraints to the generic class definition.
/ / syntactic public class MyGenericClass
< T>Where T:something
Something can be: structure, class, new (),
< 基类名>Or
< 接口名称>.
You can have multiple constraints in 1 at the same time, and the constraint itself can be a generic type.
You can also apply constraints to generic methods or delegates.
For a more in-depth understanding, see "generic programming" and "constraints on type parameters" in MSDN Library.
Example:
/ / keywords_where.cs using System; using System.Collections; struct MyStruct {/ /...} interface IMyInterface {/ /...} class MyGenericClass
< T1,T2>Where T1: IEnumerable, IMyInterface where T2: MyStruct, new () {public void MyMethod (T1, T2) {/ /...}}
C # keyword-yield
Yield is used in iterator blocks to provide values to enumerator objects or to signal the end of iteration.
/ / expression calculates and returns as an enumerator object value. The expression must be implicitly converted to the yield type of the iterator. Yield return expression; yield break
Example:
/ / keywords_yield.cs using System; using System.Collections; namespace Hunts.Keywords {public class Employee {private string _ name; private int _ id; public string Name {get {return this._name;} set {this._name = value;}} public int ID {get {return this._id } set {this._id = value;}} / / number the names in a given array public static IEnumerable SetIDs (string [] names) {int counter = 0; Employee result = new Employee (); while (counter++ < names.Length) {result._id = counter Result._name = names [counter-1]; yield return result;} class EmployeeList {static void Main () {string [] names = {"Jones", "Carl", "Dennis", "Evan", "Hugo", "Ivan"} / / display the result of the numbering operation foreach (Employee e in Employee.SetIDs (names)) {Console.WriteLine ("ID: {0} Name: {1}", e.IDCine.Name);} Console.Read ();}}
Note:
Yield statements can only appear in an iterator block, which can be used as the body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following constraints:
Unsafe blocks are not allowed.
The argument to a method, operator, or accessor cannot be ref or out.
Yield statements cannot appear in anonymous methods.
When used with expression, yield return statements cannot appear in catch blocks or in try blocks that contain one or more catch clauses.
The above is all the content of the article "how to use the keywords get, set, value, partial, where and yield in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.