����λ�ã���ҳ > �����̳� > �̳� > C#.Net����-ģʽƥ�����
��C#7��ʼ֧�ֵ� ģʽƥ�� �﷨���ǣ�ͦ�𣩣��ɷdz����Ķ����ݽ�������ƥ�����ȡ����������汾�����ƣ��Ѿ��dz�ǿ���ˡ�
��
C#7
��ʼ֧�ֵ�
ģʽƥ��
�﷨���ǣ�ͦ�𣩣��ɷdz����Ķ����ݽ�������ƥ�����ȡ����������汾�����ƣ��Ѿ��dz�ǿ���ˡ�
C# ֧�ֶ���ģʽ���������������͡���������ϵ�����ԡ��б���var ����Ԫ�ȣ���
is
��
switch
��䡢
switch
����ʽ��ʹ�ã�������ʹ�ò����߼��ؼ���
and
��
or
��
not
��϶��ģʽ������ļ��˴����д���ɶ���Ҳ��������
���� | ˵�� | ʾ��/��ע |
---|---|---|
���ͺ�����ģʽ | ������ͼ��ݣ�����������ֵ���� |
if (age is int i)
|
����ģʽ | ������ʽֵ�Ƿ���ڡ������ڣ�not������ֵ |
if(age is null || age is 0)
|
��ϵģʽ
><
|
ʹ�ù�ϵ�����
<
��
>
��
<=
��
>=
ƥ��
|
case 0 or <=6
|
�߼�ģʽ |
��
not
>
and
>
or
���Ӷ��ģʽ����ʽ
|
case < 12 and ( >6 or 6)
|
����ģʽ
{:}
|
��ʵ�������ԡ��ֶν���ģʽƥ�䣺
{����/�ֶ�:ƥ��ģʽ}
|
if (b is { Year: < 2000, Month: 1 or 11 })
|
λ��ģʽ(�⹹) |
���ڽ⹹��ֵ����ģʽƥ�䣺
(�⹹����)
|
if(point is (_,>0,>0))
|
var ģʽ |
��
var
��������������ֲ�����
|
if(point is var p && p.X>0)
|
��Ԫģʽ_ |
��Ԫģʽ
_
��ƥ���κΣ�����������ʽ
|
��ʾ��Ҫ�� |
�б�ģʽ[] |
�����飨�б�������ƥ�䣬��������
[]
��ƥ���б��е���
|
if(numbers is [_, 2, 3, ..])
|
? ģʽƥ����������﷨�ǣ�ζ����������C#�ڱ���ʱ�����Ô���Ļ������룬��ͨ�� https://sharplab.io/ ���߲鿴�����Ĵ��롣
��������Ƿ�ƥ�䣬ͬʱ����������������ͼ�������������ֵ�������ñ����ں����������������Ч��
object age = "123";
if (age is int i) //����ƥ��+��������i
{
Console.WriteLine($"age1 = {i}");
}
switch (age)
{
case string: //����ƥ��
Console.WriteLine($"type is string");
break;
case int iage: //����ƥ��+��������iage
Console.WriteLine($"age2 = {iage}");
break;
}
//����is�������Ĵ���Ч����
if (obj is int)
{
int value = (int)obj;
}
������ʽֵ�Ƿ���ڡ������ڣ�
not
������ֵ������ֵ����������������Ҳ����
const
����ֵ����ͳ��
Switch
�����dz���ģʽƥ�䡣
object age = null;
if (age is not null && age is 100) //age is 100 ��ͬ�� age is int && (int)age==100
{
Console.WriteLine($"age1 = {age}");
}
var type = age switch{
1 or 2 or 3=>"Ӥ��",
4 => "�׶�",
null or not 5 => "unknow",
_=>"",
};
><
�ù�ϵ�������ƥ�����ʽ�����ǶԳ�����ֵ���д�С�Ƚ����㣬ʹ�ù�ϵ�����
<
��
>
��
<=
��
>=
���������ʽ����
and
��
or
���ӣ���ȻҲ֧�����š�
object age = 6;
if (age is int n and >= 6)
{
Console.WriteLine("666");
}
switch (age)
{
case 0 or <=6:
Console.WriteLine("�׶�");
break;
case < 12 and ( >6 or 6):
Console.WriteLine("������");
break;
}
not
/
and
/
or
��
not
��
and
��
or
ģʽ������������߼�ģʽ�����Ӷ��ģʽ����ʽ��
not
>
and
>
or
��
(����)
��ʾ��������˳�򣬿ɶ��Ը��á�
object age = 6;
if (age is int n and (not 6 or >5) )
{
Console.WriteLine("666");
}
{:}
��ʵ�������ԡ��ֶν���ģʽƥ�䣬����Ƕ������ģʽƥ�䣬�dz���ǿ������ƥ���ô���������װ
{����/�ֶ�:ƥ��ģʽ}
��
true
ʱ�����ղŻ�ƥ��ɹ���
DateTime birthday = new DateTime(1999, 11, 12);
if (birthday is { Year: < 2000, Month: 1 or 11 })
{
Console.WriteLine("���䡢����������");
}
//Ƕ��ʹ��
public record Point(int X, int Y);
public record Segment(Point Start, Point End);
static bool IsAnyEndOnXAxis(Segment segment) =>
segment is { Start: { Y: 0 } } or { End: { Y: 0 } };
static bool IsAnyEndOnXAxis(Segment segment) =>
segment is { Start.Y: 0 } or { End.Y: 0 };
���ڽ⹹��ֵ����ģʽƥ�䣺
Tuple
��
record
��
DictionaryEntry
������֧�ֽ⹹�ģ����ڽ⹹��ֵ�ɲο�������ݡ�
()
��װ����Ҳ��
�⹹��Deconstruct��
���﷨��ʽ��
void Main()
{
Point point = new Point("sam", 12, 13);
var len = point switch
{
//����ƥ�䡢����ģʽ��λ��ģʽ��Name���Ա���Ϊstring���ҳ���Ϊ0��X��YֵΪ0
(string { Length: <= 0 }, 0, 0) => 0,
(_, > 0, 0) => point.X, //Xֵ����0��YֵΪ0
(_, 0, > 0) => point.Y, //Yֵ����0��XֵΪ0
(_, 10 or > 10, 10 or > 10) p => p.X * p.Y,
_ => 0,
};
}
public record Point(string Name, int X, int Y);
��
var
��������������ֲ��������ѱ���ʽ�Ľ�������
var
��ʱ��������������ģʽ�ı��֣����������滻����
var
��
void Main()
{
Point point = new Point("sam", 12, 13);
if(point is var p && p.X>0 && p.Y>0){ //is var
Console.WriteLine("OK");
}
var len = point switch
{
var (_,x,y) when x>0 && y>0 => true,// var
};
}
public record Point(string Name, int X, int Y);
��Ԫģʽ
��Discard Pattern��������������DZ�������û��Ҫ�ġ����Խ���Ԫģʽ������һ��ռλ������ʾһ��û���õı�������ƥ���������ͣ�����
�򻯴���
���﷨�����»��ߡ�
_
����ʾ��
���� ��
Switch
��ƥ����������ģʽ������
default
�����á�
out
������ռλ����ʾһ��û���õ�
out
������
var tuple = new Tuple(3, 4);
var (x, _) = tuple; //1��ֻ��Ҫ��һ���������������á�_����ռλ
Console.WriteLine(x); //3
_= x switch
{
2 or <2 => "small",
int and <18=>"young",
_=>"other", //2��ƥ������ģʽ��Ч��ͬdefault
};
int.TryParse("", out _); //3�����õ�out������ʵ�����������˱�����
async void Print(object arg)
{
_ = arg ?? throw new ArgumentException(); //4���������õķ��أ�Ч��ͬ��
if (arg == null) throw new ArgumentException();
_ = Task.Run(()=>Console.WriteLine("task run")); //����һ�����õķ���
}
��Ԫģʽ
_
��һ���ṩ���������õķ��ţ����߱���������������ˣ��������������������Ż��������ڶ�
out
����ʹ��ʱ�����������Զ��������������´��룺
int.TryParse("",out _);
//ʵ�ʱ����Ĵ�������
int result;
int.TryParse("", out result);
?��Ҫע����� �»���
_
�Dz�����һ���ؼ��֣�Ҳ�ܵ�����������ʹ�ã���Ҫ���á�
C#11֧�ֵģ������飨�б�������ƥ�䣬��������
[]
��ƥ���б��е��
_
��
void Main()
{
int[] numbers = { 1, 2, 3, 4 };
Console.WriteLine(numbers is [_, 2, 3, ..]); // True
Console.WriteLine(numbers is [0 or 1, <= 2, >= 3]); // False
}
����ĸ���ģʽƥ����Ҫ������ is ����� �� switch ��� �� switch ����ʽ �С�
is ����� ������Ҫ������������ͼ����Եģ�����ģʽƥ�����������ֻ����ˣ���������ø��ּ����Ĵ��롣
object value = 12;
if (value is int && value is not null) //is���ͼ��+�߼�ģʽ
{
Console.WriteLine(value);
}
if (value is int a && a > 6) //+����ģʽ
{
Console.WriteLine(a);
}
if (value is int age and > 10 and < 14) //��ϵģʽ
{
Console.WriteLine(age);
}
var user = new { Name = "sam", Age = 12 };
if (user is { Name: _, Age: > 10 }) //����ģʽ
{
Console.WriteLine(user.Name);
}
int[] arr = new int[] { 1, 2, 3 };
if (arr is [> 0, ..]) //�б�ģʽ����һ��Ԫ��>0
{
Console.WriteLine(arr);
}
var dt = new Tuple("sam", 100);
if (dt is (_, > 60) d) //λ��ģʽ+����ģʽ������ûʲô�ã�
{
Console.WriteLine(d.Item1);
}
switch..case ��� �Ǻܶ������ж��еĻ�����������֧��䣬��ͳ�� case ֻ������ƥ�䳣����������ö�١�
case
���ܴ�͸��һ��
case
ִ��������
break
����������
return
���أ��˳������������Զ��
case
ƥ��ִ��һ���߼����롣
case
���dz���ģʽ�����ִ���
case
���Խ���������ģʽʹ�ã��dz�ǿ��
when
�����ɸ��Ӹ���������
int age = 22;
string sex = "Male";
switch (age)
{
case 1:
case 2:
Console.WriteLine("Ӥ��");
break;
case <= 3:
Console.WriteLine("�׶�");
break;
case > 10 and < 16:
Console.WriteLine("������");
break;
case > 18 when sex == "Male":
Console.WriteLine("��������");
break;
case int:
break;
}
C#8
��
switch
�����µ��﷨ ����
switch ����ʽ
�����Կ�����
switch..case
����һ�����֣�ʹ�ñȽ����ơ�
switch
����ʽ��һ����ֵ���������䡣
=>
���Ϊģʽ������һ��bool�������ģʽƥ�䣨true���򷵻��Ҳ��ֵ�����һ����Ԫģʽƥ�����������ͬ
default
����
int type = 6;
var message = type switch
{
<= 1 => "success",
2 => "warning",
3 => "error",
> 3 and < 10 => "other error",
_ => "unkonwn error",
};
������
when
�����и�����жϣ�
when
����ı���ʽ�ͺ������ˣ�ֻҪ����
boo
���ɡ�
object type = 6;
var message = type switch
{
int i when i<6 => "ok",
string s when s=="null"=>"Null",
string s when !string.IsNullOrEmpty(s)=>"string value",
_=>"unknown value"
};
Console.WriteLine(message);
֧�ֶ�����������ģʽ��������
()
�����������
string gender = "male";
int age = 10;
string type = (gender,age) switch{
("male",>18)=>"VIP",
(not "male",>26 and <35)=>"VVIP",
_=>"",
};
??��Ȩ���� ����Ȩ����@��ľϦ���������ݽ���ѧϰ����Óָ����������ת����ע�������� Ô�ıà¼ï¿½ï¿½Ö·-��ȸ
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������