什么是C#中使用`default`关键字? [英] What is the use of `default` keyword in C#?

查看:115
本文介绍了什么是C#中使用`default`关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 什么是C#中使用默认关键字?

  2. 是它推出的C#3.0?


解决方案

默认关键字的上下文,因为它具有多种用途。我猜测你是指在它返回一个类型的默认值其新C#2的意思。对于引用类型,这是和值类型这个新实例的所有zero'd了。

下面是一些例子来证明我的意思是:

 使用系统;类示例
{
    静态无效的主要()
    {
        Console.WriteLine(默认值(的Int32)); //输出0
        Console.WriteLine(默认值(布尔)); //输出假
        Console.WriteLine(默认值(字符串)); //输出什么(因为它为null)
    }
}

  1. What is the use of default keyword in C#?
  2. Is it introduced in C# 3.0 ?

解决方案

The default keyword is contextual since it has multiple usages. I am guessing that you are referring to its newer C# 2 meaning in which it returns a type's default value. For reference types this is null and for value types this a new instance all zero'd out.

Here are some examples to demonstrate what I mean:

using System;

class Example
{
    static void Main()
    {
        Console.WriteLine(default(Int32)); // Prints "0"
        Console.WriteLine(default(Boolean)); // Prints "False"
        Console.WriteLine(default(String)); // Prints nothing (because it is null)
    }
}

这篇关于什么是C#中使用`default`关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆