CSharp语法

CSharp5.0

CallerMemberName, CallerFilePath, 和 CallerLineNumber 是编译时常量,它们是csharp5.0引入的特性,用于提供有关调用堆栈的信息,通常用于日志记录和调试。这些特性可以自动填充方法的参数,无需显式传递信息。

/// <summary>
/// 调用本方法,是哪个类中是哪个方法中的的第几行代码来调用本方法;
/// </summary>
/// <param name="message"></param>
/// <param name="memberName"></param>
/// <param name="sourceFilePath"></param>
/// <param name="sourceLineNumber"></param>
public void TraceMessage(string message,
	  [CallerMemberName] string memberName = "",
	  [CallerFilePath] string sourceFilePath = "",
	  [CallerLineNumber] int sourceLineNumber = 0)
{
	Console.WriteLine("message: " + message);
	Console.WriteLine("member name: " + memberName);
	Console.WriteLine("source file path: " + sourceFilePath);
	Console.WriteLine("source line number: " + sourceLineNumber);
}

CSharp6.0&VS2015

静态类方法

StudentServie.StudyStatic()

c#6.0语法:

//引入带有静态方法&属性的类

using static csharp_six.StudentServie;

StudyStatic(); //来自于StudentServie 的静方法

引入枚举

//定义枚举
public enum Color
{
	Red,
	Green,
	Blue
}

//引用枚举
using static namespace.Color

//使用的时候
var red = Red;
var green = Green;
var blue = Blue;

using泛型类别名

//定义泛型类
public class GenericClass<T>
{
	public void Show(T t)
	{
		Console.WriteLine($"T name is {typeof(T).FullName}");
	}
}

//引入泛型类
using Sharp6genericeInt = namespace.GenericClass<int>;
using Sharp6genericeString = namespace.GenericClass<string>;
using Sharp6genericeDatetime = namespace.GenericClass<System.DateTime>;

//使用泛型类
Sharp6genericeInt  aaa = new Sharp6genericeInt();
Sharp6genericeString  bbb = new Sharp6genericeString();

异常筛选器

//异常捕捉筛选
{
	try
	{
		ShowExceptionType();
	}
	catch (Exception e) when (e.Message.Contains("002"))
	{
		Console.WriteLine("这里是处理捕捉到的002的异常~");
	}
}

CSharp7.0&VS2017

元组简单化

 //元组
 Tuple<double, int> ttext = new Tuple<double, int>(123, 456);
 Console.WriteLine(ttext.Item1);
 Console.WriteLine(ttext.Item2);

//元组简单化
(double, int) ttext1 = (123, 456);
Console.WriteLine(ttext1.Item1);
Console.WriteLine(ttext1.Item2);

元组用例

var xs = new[] { 4, 7, 9 };
var limits = FindMinMax(xs);
Console.WriteLine($"Limits of [{string.Join(" ", xs)}] are {limits.min} and {limits.max}");


public static (int min, int max) FindMinMax(int[] input)
{
	if (input is null || input.Length == 0)
	{
		throw new ArgumentException("Cannot find minimum and maximum of a null or empty array.");
	}

	var min = int.MaxValue;
	var max = int.MinValue;
	foreach (var i in input)
	{
		if (i < min)
		{
			min = i;
		}
		if (i > max)
		{
			max = i;
		}
	}
	return (min, max);
}

元组字段名称

//元组字段名称
{
	var t = (Sum: 4.5, Count: 3);
	Console.WriteLine(t.Sum);
	Console.WriteLine(t.Count);

	(double Sum, int Count) d = (5.5, 6);
	Console.WriteLine(d.Sum);
	Console.WriteLine(d.Count);
}

元组赋值和析构

 //元组赋值和析构
 {
     (int, double) t1 = (17, 3.14);
     (double First, double Second) t2 = (0.0, 1.0);
     t2 = t1;
     Console.WriteLine($"{nameof(t2)}:{t2.GetType} {t2.First} and {t2.Second}");
 }

使用现有变量

//使用现有变量
{
	string destination = string.Empty;
	var distance = 0.0;
	(destination, distance) = ("post office", 10);
	Console.WriteLine($"Distance to {destination} is {distance} kilometers.");
}

元组作为 out 参数

//dictionary初始化声明
var limitsLookup = new Dictionary<int, (int Min, int Max)>()
{
	{1,(2,4)},
	{2,(5,6)},
	{3,(7,8)}
};
//dictionary初始化声明
var limitsLookup1 = new Dictionary<int, (int Min, int Max)>()
{
	[2] = (4, 10),
	[4] = (10, 20),
	[6] = (0, 23)
};

limitsLookup.TryGetValue(1, out var limits);
limitsLookup1.TryGetValue(2, out var limits1);

模式匹配,null检查

int? maybe = 12;
if (maybe is int number)
{
    Console.WriteLine($"The nullable int 'maybe' has the value {number}");
}
else
{
    Console.WriteLine("The nullable int 'maybe' doesn't hold a value");
}

解析离散值

//switch的特殊写法
public static string PerformOperation(string command) => command switch
{
	"A" => "a",
	"B" => "b",
	"C" => "c",
	"D" => "d",
	_ => throw new ArgumentException("Invalid string value for command", nameof(command))
};

//另外一直用法
public static string WaterState(int tempInFahrenheit) => tempInFahrenheit switch
{
	(> 32) and (< 212) => "liquid",
	< 32 => "solid",
	> 212 => "gas",
	32 => "solid/liquid transition",
	212 => "liquid / gas transition",
};

弃元

public static (string, double, int, int, int, int) QueryCityDataForYears(string name, int year1, int year2){
    return (name, area, year1, population1, year2, population2);
}

//_接收的元组数组被忽略掉,只有pop1和pop2被接受
var (_, _, _, pop1, _, pop2) = QueryCityDataForYears("New York City", 1960, 2010);
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值