In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Xiaobian to share with you what new features of C#3.5, I believe most people still do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
Implicity-typed Local Variables is a syntax form in which the compiler automatically infers the type of a variable when it is declared. It declares variables using the var keyword. For example:
var a = 1; var b = "Hello, Linq! "; var c = 2.23;
The compiler automatically assigns its variable type based on type inference, equivalent to the following declaration form:
int a = 1; string b = "Hello, Linq"; decimal c = 2.23;
Note that variables declared with the var keyword are not equivalent to variables declared with object. A variable declared with var is inferred from its type, and its type at compile time and runtime is the true type of the variable itself; a variable declared with object is of type System.Object at compile time, accompanied by an implicit type conversion process.
C#3.5 New feature: Object and collection initializers
This syntax was created to simplify initialization assignment operations for object and collection types, such as arrays. For example, the following code declares and initializes a List
< string>。
List
< string>list = new List
< string>(); list.Add("This"); list.Add("Is"); list.Add("A"); list.Add("Collection");
You can initialize directly using the following methods:
var list = new List
< string>() ... { "This", "Is", "A", "Collection" };
Collection initializers can be used on any type that implements the Add method. The following example shows how to create such a type.
public class Persons ... { private List
< string>list = new List
< string>(); public void Add(string name) ...{ list.Add(name); } static void Main() ...{ var p = new Persons() ...{ "1", "2", "3" }; }
对象初始值设定项则可以更加直接的初始化一个对象的实例,例如对于 Person 类,有公开的 Name, Age 和 Height 属性,在实例化 Person 的时候,可以用如下语法形式。
public class Person ...{ public string Name ...{ get; set; } public int Age ...{ get; set; } public decimal Height ...{ get; set; } } var p = new Person ...{ Name = "Orochi", Age = 24, Height = 175 }; var persons = new[] ...{ new Person ...{ Name = "Orochi", Age = 24, Height = 175 }, new Person ...{ Name = "Blinda", Age = 23, Height = 165 }, new Person ...{ Name = "Ninicat", Age = 22, Height = 170 } };
代码中 persons 的类型被推断为 Person[]。
C# 3.5新特性:匿名类型
匿名类型常常用在查询表达式的结果中,因为这种类型的返回值往往是一个包含一种特定类型的 IEnumerable
< T>。For example, to select the set of Persons older than 21 years and taller than 160 cm from the above example, it would take the following form.
var result = from person in persons where person.Age >= 21 && person.Height >= 160 select new ... { Name = person.Name, Age = person.Age, Height = person.Height / 100 }; new { Name = person.Name, Age = person.Age, Height = person.Height / 100 } is an anonymous type that the compiler declares as follows: public class _Anonymous_Name_Age_Height ... { public string Name; public string Age; public decimal Height; }
C#3.5 New Features: Extended Methods
An extension method introduces a method implemented on a particular type onto that type and can be invoked directly with that type.
For example, Count() method can count the number of elements, Count() method can be implemented in string, array, set, IEnumerable
< T>Even on the Persons class defined above. To implement Count() on string, you can use the following code:
using System.Runtime.CompilerService; public class Extensions ... { [Extension()] public int Count(this string source) ... { int count = 0; foreach (var item in source) ... { count++; } return count; } [Extension()] public int Count
< T>(this IEnumerable
< T>source) ... { int count = 0; foreach (var T in source) ... { count++ } return count; } }
So, in IEnumerable.
< T>Count() method is implemented on both on and string. We can use IEnumerable like
< T>Use extension methods just like member methods on string, for example:
string s = "Hello, World! "; int c1 = s.Count(); List
< int>list = new List
< int>() ... { 1, 2, 3, 4, 5, 6 }; int c2 = list.Count();
C#3.5 New feature: Lambda expression
Lambda expressions are anonymous function constructs that facilitate the initialization of delegate type parameters for delegate, query synthesis, and extension methods. For example:
delegate void Func(int x); void Add(int x) ... { x ++; } Func f = new Func(Add); f(1);
F can be instantiated in a more concise way.
Func f = (x) => ... { x++; };
or
Func f = (int x) => ... { x++; };
Although the above code doesn't make much sense in practice, it shows us a more intuitive way to implement delegation. The basic syntax for Lambda expressions is:
([[
< 类型>]
< 变量名>[, [
< 类型>]
< 变量名>]]) => {
< 语句快>};
Lambda expressions can have no argument list, such as:
() => ... { Console.WriteLine(""); };
C#3.5 New feature: loose delegation
Relaxed delegation allows C#to accept functions with different signatures when determining delegation instantiation assignments. For example, EventArgs and MouseEventArgs are classes with an inheritance relationship, and the compiler accepts both types of delegates when they appear in the same delegate definition that accepts EventArgs type parameters. For example:
delegate void A (object sender, MouseEventArgs e); delegate void B (int a, int b); EventHandler e1, e2; e1 = new A(...); // OK e2 = new EventHandler(...); // OK e1 = e2; // OK B b = (long a, int b) +> ... { }; // OK
C#3.5 new feature: automatic implementation of attributes
When defining attributes of a class, it is often necessary to encapsulate a field like the following code.
private string name; public string Name ... { get ... { return name; } set ... { name = value; } }
C#3.0 provides a simplified approach to attribute definition that does what the code does.
public string Name ... { get; set; }
This is the automatic implementation attribute. The compiler automatically implements code similar to domain encapsulation. However, auto-implemented attributes cannot define read-only and get-only attributes; nor can sets have accessibility descriptions.
C#3.5 New feature: Partitioned approach
Partial methods allow developers to define methods of a class in multiple files. For example:
//file 1.cs public partial class A... { void B(); } //file 2.cs public partial class A... { void B ... { Console.WriteLine("B invoked. "); } }
This syntax allows you to write function definitions and declarations separately. Note that the divisional approach requires:
The class entity of a partial method must be partial.
The return value of a partial method must be void.
If a partial method is not implemented, but a declaration of this method is defined, the compiler automatically removes the unimplemented method signature when using the class that contains the partial method.
The above is all the content of "What's New in C#3.5", thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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.