当前位置:网站首页>C 10 new features_ C 10 new features
C 10 new features_ C 10 new features
2022-06-13 02:54:00 【Tianma 3798】
C# 10 New function _C# 10 New features _ Changes in grammar
One 、C# 10 towards C# The following features and enhancements have been added :
Record structure
Improvement of structure type
Interpolation string handler
Instructions
File scoped namespace declaration
Extended attribute mode
Yes Lambda Improvement of expression
You can use Interpolation string
The record type can be sealed
Improved explicit assignment
Assignment and declaration can be performed simultaneously in the same destructor
Can be used on the method attribute
CallerArgumentExpression attribute
Enhanced pragma
Click reference for more details :
C# 10 What's new in - C# guide | Microsoft Docs
Two 、struct Add grammar
record struct
First, of course record struct, It's solved record Only for class And can't give struct Use questions :
record struct Point(int X, int Y);
use record Definition struct There are actually many benefits , For example, you don't have to rewrite GetHashCode and Equals And so on .
sealed record ToString Method
Before record Of ToString It can't be modified as sealed Of , So if you inherit a record, Corresponding ToString Behavior can also be changed , So this is a virtual method .
But now you can put record Inside ToString Method is marked as sealed, So your ToString Methods will not be overridden .
struct Parameter free constructor
all the time struct Parameterless constructors are not supported , Now it supports :
struct Foo
{
public int X;
public Foo() { X = 1; }
}
But when you use it, you should pay attention to , Because the existence of parameterless constructors makes new struct() and default(struct) The semantics of are different , for example new Foo().X == default(Foo).X In the above example, we will get false.
3、 ... and 、 Of anonymous objects with
It can be used with To create a new anonymous object based on an existing anonymous object :
var x = new { A = 1, B = 2 };
var y = x with { A = 3 };
here y.A Will be 3 .
Four 、 Overall using
Take advantage of the overall situation using You can enable... For the entire project usings, No longer need to write a copy of every document . For example, you can create a Import.cs, And then it says :
global using System;
global using i32 = System.Int32;
Then you don't have to using System, And you can use i32 了 .
5、 ... and 、 Document scope namespace
This is simpler , I used to write namespace And a layer of braces , In the future, if there is only one in a file namespace Words , Then just write it directly at the top :
namespace MyNamespace;6、 ... and 、 Constant string interpolation
You can give it const string Using string interpolation , Very convenient :
const string x = "hello";
const string y = $"{x}, world!";7、 ... and 、lambda Expression improvement
1. Support attributes
lambda You can take attribute 了 :
f = [Foo] (x) => x; // to lambda Set up
f = [return: Foo] (x) => x; // to lambda Return value setting
f = ([Foo] x) => x; // to lambda Parameter setting
2. The specified return value type is supported
before C# Of lambda The type of return value depends on derivation ,C# 10 Start by allowing the specified... To appear at the top of the parameter list lambda Type :
f = int () => 4;
3. Support ref 、in 、out And so on
f = ref int (ref int x) => ref x; // Returns a reference to a parameter
4. First class functions
Functions can be implicitly converted to delegate, So the function goes up to the first-class function :
void Foo() { Console.WriteLine("hello"); }
var x = Foo;
x(); // hello
5. Natural delegate type
lambda The natural delegate type is now created automatically , So there is no need to write out types .
var f = () => 1; // Func<int>
var g = string (int x, string y) => $"{y}{x}"; // Func<int, string, string>
var h = "test".GetHashCode; // Func<int>
CallerArgumentExpression
Now? ,CallerArgumentExpression This attribute It's finally working . With this help attribute, The compiler automatically fills in the expression string of the calling parameter , for example :
void Foo(int value, [CallerArgumentExpression("value")] string? expression = null)
{
Console.WriteLine(expression + " = " + value);
}
When you call Foo(4 + 5) when , Will be output 4 + 5 = 9. This is extremely useful for testing frameworks , Because you can output assert The original expression of :
static void Assert(bool value, [CallerArgumentExpression("value")] string? expr = null)
{
if (!value) throw new AssertFailureException(expr);
}8、 ... and 、tuple Support mixed definition and use
int y = 0;
(var x, y, var z) = (1, 2, 3);
therefore y It becomes 2 了 , Two variables are also created x and z, Namely 1 and 3 .
Nine 、 Generic attribute
Yes, you didn't read it wrong ,C# Of attributes Support generics , however .NET 6 Will preview a specific release , Therefore need <LangVersion>preview</LangVersion>:
class TestAttribute<T> : Attribute
{
public T Data { get; }
public TestAttribute(T data) { Data = data; }
}
Then you can use it :
[Test<int>(3)]
[Test<float>(4.5f)]
[Test<string>("hello")]More syntax changes await later exploration .....
more :
.Net 6.0 New features in _.Net 6.0 What's new in
Element-ui and Element-Plus The difference between _Element2 and Element3 The difference between
Vue2 and Vue3 The difference between _Vue3 and Vue2 The difference between
Vue3+Element Plus Develop and build _Vue3+Element3 Develop and build
边栏推荐
- PCR validation of basic biological experiments in [life sciences]
- Radio design and implementation in IVI system
- Ijkplayer source code -- mnatemediaplayer of ijkmediaplayer
- too old resource version,Code:410
- Digital IC Design -- FIFO design
- Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
- Graduation project - campus old thing recycling system based on stm32
- Opencv 07, pixel read, change and bitmap write
- Hash table: least recently used cache
- IOS interview · full bat interview record of an IOS programmer (including the true interview questions of Baidu + Netease + Alibaba)
猜你喜欢

Introduction and download of common data sets for in-depth learning (with network disk link)

The weight of the input and textarea components of the applet is higher than that of the fixed Z-index

Introduction to facial expression recognition system -- offline environment configuration
![[data analysis and visualization] key points of data drawing 9- color selection](/img/76/0d707b3d2446b5cd8002cbc2834c5c.jpg)
[data analysis and visualization] key points of data drawing 9- color selection

六款国产飞机专用GPU产品通过鉴定审查

For loop instead of while loop - for loop instead of while loop

Matlab: find the inner angle of n-sided concave polygon

Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)

Graduation project - campus old thing recycling system based on stm32

冲刺强基计划数学物理专题一
随机推荐
Stack: daily temperature
For loop instead of while loop - for loop instead of while loop
Summary of the latest IOS interview questions in June 2020 (answers)
js 解构赋值
How to manage the IT R & D department?
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
Ijkplayer source code - setting option 2
Opencv 08 demonstrates the effect of opening and closing operations of erode, dilate and morphological function morphologyex.
Using binary heap to implement priority queue
House raiding
数仓笔记|针对客户维度建模需要关注的5个因素
Image classification system based on support vector machine (Matlab GUI interface version)
Bi modal progressive mask attention for fine graded recognition
How did you spend your winter vacation perfectly?
Spark UDF instance details
01 initial knowledge of wechat applet
Linked list: reverse linked list
Matlab: find the inner angle of n-sided concave polygon
Summary of innovative ideas of transformer model in CV
Retrofit easy to use