当前位置:网站首页>C# default
C# default
2022-07-18 13:20:00 【Poetry and distance】
One 、switch sentence
stay switch In the sentence , If there isn't any case The expression matches the switch value , Then control is passed to the following optional default The statement after the tag . without default label , Then control passes to switch outside .
switch (key){
case 1:
Console.WriteLine("1");
break;
case 2:
Console.WriteLine("2");
break;
default:
Console.WriteLine("Sorry, no one matches!");
break;
}
Two 、 Generic code
The reason why it is used default keyword , It's because you don't know whether the generic type parameter is a value type or a reference type , The initial value of the object is . Consider the following code :
class TestDefault<T>
{
public T foo()
{
// Here is uncertainty T Is it a value type or a reference type ,int i = null (?)or class i = null (?)
T t = null;
return t;
}
}
If we use int Type to bind generic parameters , that T Namely int type , Then the line of the comment becomes int t = null; Obviously, this is meaningless . To solve this problem , Introduced default keyword :
Just to give T This unknown type of parameter is assigned a default value
class TestDefault<T>
{
public T foo()
{
return default(T);
}
}
边栏推荐
- Yiwen xuxue pyspark data analysis foundation: Spark local environment deployment and construction
- 工作方法记录
- ReversingKr-wp(5)
- 迁移学习在医学图像分类中的研究进展
- 剑指 Offer 10- I. 斐波那契数列
- Mysql5.7创建用户错误:ERROR 1364 (HY000): Field ‘ssl_cipher‘ doesn‘t have a default value解决方法
- [machine learning] PCA - principal component analysis
- ReversingKr-wp(4)
- 剑指 Offer 27. 二叉树的镜像
- USB 协议 (一)
猜你喜欢
随机推荐
剑指 Offer 28. 对称的二叉树
Hcip day 6 notes
160_ Skills_ Power Bi new function - calculate working days
[programming compulsory training 9] the number of schemes in the grid (recursion) + alternative addition (bit operation)
ReversingKr-wp(4)
es查询案例
Today's job insights
TCP/IP之常用协议
Use of resttemplate
解决pycharm无法输入中文的方法:
剑指 Offer 10- I. 斐波那契数列
[sword finger offer] print the linked list (stack idea) + repeated numbers in the array (hash mapping) from end to end
How to solve pycharm's inability to input Chinese:
SSH local port forwarding
[machine learning decision tree] information gain
剑指 Offer 10- II. 青蛙跳台阶问题
内存管理页面属性
Sword finger offer 47 Maximum value of gifts
Theoretical basis of zoom tracking
QA机器人第一节——介绍


![[machine learning] integrated learning - ensemble learning](/img/35/3d34497af77247f7ab79376e1abde9.png)




