C # features

Source: Internet
Author: User

  • Yield

It is usually used in the iterator to provide a value to the IEnumerable object or end the iteration.

For example:

yield return expression; yield break;
  • Var

Defines variables of the implicit type.

var i = 5; var s = "Hello";

 

Note that the implicit type (Implicitly typed) is not a "dynamic type" and is determined by the compiler during compilation.

  • Default

In addition to providing default conditions in the switch statement, default is also used as a keyword in generics. For example:

T temp = default(T);

Because we do not know whether the T type is a reference or a value type, it cannot be used to compare it with null (only the reference type is available) or a number (the value type can be compared with it. If the default keyword is used, null is returned for the reference type, and 0 is returned for the value type.

Global ::

Keyword global: Used to reference a global namespace.

class TestClass : global::TestApp { }

Volatile

This field can be modified by multiple threads simultaneously in multiple threads. For more examples about how to use volatile multithreading, refer to how to: create and terminate a thread.

Public volatile int I; // C # can use the extern modifier to declare external implementation methods. // It is often used in Interop services to use unmanaged code together with the DllImport attribute (static must be declared at the same time) // For example: [DllImport ("avifil32.dll")] private static extern voidAVIFileInit (); // C # Can reference an assembly with the same type name (multiple versions of the same control) at the same time. // use an external command line to specify the alias reference at this time. // For example: /r: GridV1 = grid. dll/r: GridV2 = grid1_dll // when using them in the program // you need to use the key extern to reference them: extern alias GridV1; extern alias GridV2; // GridV1 and GridV2 will be referenced and the global namespace will have additional space at the same level // use GridV1: Grid or GridV2: Grid to get different version Grid types. // Syntax ?? The null merge operator is used to define the default value of the reference type that can be null. // If the left operand is not null, the left operand is returned. Otherwise, the right operand is returned. Int y = x ?? -1; // It is also a null Operator: static int? GetNullableInt () {return null;} //... or int? X = null; where T: new () // we know that you can use the where keyword to restrict generic definitions. // For example, public class MyClass <T, U> where T: class where U: struct {} // At the same time, we can include the constructor constraints in the generic definition // new () the constraint tells the compiler that any type of parameter contains a non-parameter (default) constructor. Public class MyGenericClass <T> where T: IComparable, new () {T item = new T ();} // language features Nullable type when we want to describe a value type that may not exist // use nullable to replace the value type // use? Modifier to declare an nullable type // such as: int? A = 1; // C # The Nullable class and Nullable struct are supported to support the nullable type. // The Nullable struct contains two useful attributes, HasValue and Value, to determine and obtain whether the current type has another Value. // Curryization may be a bit unfamiliar with. NET friends. // wikipedia translates the name as "kerryization"-I personally don't like it, but currying is used in English. // Curry is an implementation technology of functional programming. // The general process is to convert the function that originally accepts multiple parameters into a function with only the first parameter, and then return the new function to accept the remaining parameters. // A bit of interface, in addition, there are relatively few Chinese documents about functional programming or curry. // functional programming and curry interpretation/all languages that can implement closures can achieve curry. // In C #2.0, it can be implemented through anonymous delegation. In 3.0, curry is relatively simple: static class Program {static Func <TArg1, Func <TArg2, TResult> Curry <TArg1, TArg2, TResult> (this Func <TArg1, TArg2, TResult> f) {return a1 => a2 => f (a1, a2 );} static voidMain () {Func <int, int, int> add = (x, y) => x + y; var curriedAdd = add. curry (); Console. writeLine (curriedAdd (13) (29) ;}// interested in implementing functional programming using C # // more Code implementations can be found in the MSDN Code. // The anonymous type can be used to encapsulate a set of read-only attributes into a single object without explicitly defining a type. // The type name is generated by the compiler and cannot be used at the source code level. // The type is also determined by the compiler's inference. // Usually used with the select clause of the LINQ query expression // initialize the object var productQuery = from prod in products select new {prod. color, prod. price}; // create a new object, foreach (var v in productQuery) {Console. writeLine ("Color = {0}, Price = {1}", v. color, v. price);} // Extension Method C #3.0 can expand the type of CLR // For example, you may need to obtain the number of words in an English string // you can write a method to process the string in the past // now you can add a method to calculate the number of words for the string type: namespace ExtensionMethods {public static class MyE Xtensions {public static int WordCount (this String str) {returnstr. Split (new char [] {'', '.', '? '}, StringSplitOptions. removeEmptyEntries ). length ;}}// use this to extend the WordCount Method to the CLR built-in type. // you can use it directly like a common method: using ExtensionMethods ;//... string s = "Hello Extension Methods"; int I = s. wordCount (); // For more information, see the MSDN or ScottGu blog. // Method and attribute List. ForEach () Array or List has a very practical ForEach method // you can directly input a method to operate on elements in the set. // For example, List <String> names = new List <String> (); names. add ("Bruce"); names. add ("Alfred"); names. forEach (Print); names. forEach (delegate (String name) {Console. writeLine (name) ;}); private static void Print (string s) {Console. writeLine (s) ;}// GetValueOrDefault for nullable type objects // in addition to the two common attributes Value and HasValue // You can also use the GetValueOrDefault method to obtain the current Value or default Value: float? MySingle = 12.34f; float? YourSingle =-1.0f; yourSingle = mySingle. GetValueOrDefault (-222.22f); // yourSingle = 12.34 mySingle = null; yourSingle = mySingle. GetValueOrDefault (); // yourSingle = 0

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.