string ddd = ""; //先申明变量 ccc.StringOut(out ddd); Console.WriteLine(ddd);
StringOut(out string ddd); //传递的同时申明 Console.WriteLine(ddd); Console.ReadLine();
static void Main(string[] args)
{
var data = GetFullName();
Console.WriteLine(data.Item1);
Console.WriteLine(data.Item2);
Console.WriteLine(data.Item3);
Console.ReadLine();
}
static Tuple<string, string, string> GetFullName()
{
return new Tuple<string, string, string>("a", "b", "c");
}
static void Main(string[] args)
{
var data=GetFullName();
Console.WriteLine(data.a); //可用命名获取到值
Console.WriteLine(data.b);
Console.WriteLine(data.c);
Console.ReadLine();
}
//方法定义为多个返回值,并命名
private static (string a,string b,string c) GetFullName()
{
return ("a","b","c");
}
static void Main(string[] args)
{
//定义解构元组
(string a, string b, string c) = GetFullName();
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.ReadLine();
}
private static (string a,string b,string c) GetFullName()
{
return ("a","b","c");
}
object a = 1;
if (a is int) //is判断
{
int b = (int)a; //拆
int d = b+10; //加10
Console.WriteLine(d); //输出
}
object a = 1;
if (a is int c) //这里,判断为int后就直接赋值给c
{
int d = c + 10;
Console.WriteLine(d);
}
static dynamic Add(object a)
{
dynamic data;
switch (a)
{
case int b:
data=b++;
break;
case string c:
data= c + "aaa";
break;
default:
data = null;
break;
}
return data;
}
object a = 1; var data= Add(a); Console.WriteLine(data.GetType()); Console.WriteLine(data);
object a = "bbbb"; var data= Add(a); Console.WriteLine(data.GetType()); Console.WriteLine(data);
switch (a)
{
case int b when b < 0:
data = b + 100;
break;
case int b:
data=b++;
break;
case string c:
data= c + "aaa";
break;
default:
data = null;
break;
}
public static void DoSomeing()
{
//调用Dosmeing2
int data = Dosmeing2(100, 200);
Console.WriteLine(data);
//定义局部函数,Dosmeing2.
int Dosmeing2(int a, int b)
{
return a + b;
}
}
public void CreateCaCheContext() => new CaCheContext();
//等价于下面的代码
public void CreateCaCheContext()
{
new CaCheContext();
}
// 构造函数的表达式写法
public CaCheContext(string label) => this.Label = label;
// 析构函数的表达式写法
~CaCheContext() => Console.Error.WriteLine("Finalized!");
private string label;
// Get/Set属性访问器的表达式写法
public string Label
{
get => label;
set => this.label = value ?? "Default label";
}
public string IsNull()
{
string a = null;
if (a == null)
{
throw new Exception("异常了!");
}
return a;
}
public string IsNull()
{
string a = null;
return a ?? throw new Exception("异常了!");
}
public class CaCheContext
{
public ValueTask<int> CachedFunc()
{
return (cache) ? new ValueTask<int>(cacheResult) : new ValueTask<int>(loadCache());
}
private bool cache = false;
private int cacheResult;
private async Task<int> loadCache()
{
// simulate async work:
await Task.Delay(5000);
cache = true;
cacheResult = 100;
return cacheResult;
}
}
//main方法可不能用async修饰,所以用了委托.
static void Main(string[] args)
{
Action act = async () =>
{
CaCheContext cc = new CaCheContext();
int data = await cc.CachedFunc();
Console.WriteLine(data);
int data2 = await cc.CachedFunc();
Console.WriteLine(data2);
};
// 调用委托
act();
Console.Read();
}
int a = 123_456; int b = 0xAB_CD_EF; int c = 123456; int d = 0xABCDEF; Console.WriteLine(a==c); Console.WriteLine(b==d); //如上代码会显示两个true,在数字中用"_"分隔符不会影响结果,只是为了提高可读性
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有