zoukankan      html  css  js  c++  java
  • "default" Keyword: An Interesting Statement

    I noticed an interesting statement when skimming through Windows Workflow Foundation Hands-On Lab code, it looks like:

    public int amount = default(System.Int32);

    Now I feel a little bit not comfortable  with the "default" here. Look it up (from MSDN):

    In generic classes and methods, one issue that arises is how to assign a default value to a parameterized type T when you do not know the following in advance:

    • Whether T will be a reference type or a value type.

    • If T is a value type, whether it will be a numeric value or a struct.

    Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.

    Now it starts to make sence why I don't feel very right here...

    Copy a question from thescripts.com, maybe a good question for your next interview:

    What's the difference among the following statements?

    int x = default(int);
    int x = 0;
    int x = new int();

  • 相关阅读:
    可视化数据库管理工具DataGrip使用详解
    MySQL常用函数
    你必须掌握的 21 个 JAVA 核心技术!
    idea中那些好用到飞起的插件
    Object使用
    单页面应用和多页面应用的区别及优缺点
    正则常用匹配
    npm --save-dev 和 --save 的区别
    js常用小技巧
    js复制文字到剪切板
  • 原文地址:https://www.cnblogs.com/Hal/p/850114.html
Copyright © 2011-2022 走看看