简体   繁体   English

int字段的默认值是0吗?

[英]Does an int field have a default of 0?

I have a Console app with the following code: 我有一个控制台应用程序,其代码如下:

    using System;

    namespace HeadfirstPage210bill
    {
        class Program
        {
            static void Main(string[] args)
            {
                CableBill myBill = new CableBill(4);
                Console.WriteLine(myBill.iGotChanged);
                Console.WriteLine(myBill.CalculateAmount(7).ToString("£##,#0.00"));
                Console.WriteLine("Press enter to exit");
                Console.WriteLine(myBill.iGotChanged);
                Console.Read();
            }
        }
    }

The class CableBill.cs is the following: CableBill.cs类如下:

    using System;

    namespace HeadfirstPage210bill
    {
        class CableBill
        {
            private int rentalFee;
            public CableBill(int rentalFee) {
                iGotChanged = 0;
                this.rentalFee = rentalFee;
                discount = false;
            }

            public int iGotChanged = 0;


            private int payPerViewDiscount;
            private bool discount;
            public bool Discount {
                set {
                    discount = value;
                    if (discount) {
                        payPerViewDiscount = 2;
                        iGotChanged = 1;
                    } else {
                        payPerViewDiscount = 0;
                        iGotChanged = 2;
                    }
                }
            }

            public int CalculateAmount(int payPerViewMoviesOrdered) {
                return (rentalFee - payPerViewDiscount) * payPerViewMoviesOrdered;
            }

        }
    }

The console is returning the following: 控制台返回以下内容:

在此输入图像描述

What I can not see is when payPerViewDiscount is set to 0. Surely this can only happen when the Discount property is set but if the property Discount is called then the variable iGotChanged should be returning 1 or 2, but it seems to stay on 0. Because it is type int is there a default value for payPerViewDiscount of 0? 我看不到的是payPerViewDiscount设置为0.当然,这只能在设置Discount属性时发生,但如果调用属性Discount,则变量iGotChanged应该返回1或2,但它似乎保持为0。因为它是int类型, payPerViewDiscount的默认值是0吗?

yes the default value of int is 0 you can check using the default keyword 是的,int的默认值为0您可以使用default关键字进行检查

int t = default(int);

t will hold 0 t将保持0

Fields in a class are initialized to their default value before the constructor runs. 在构造函数运行之前,类中的字段将初始化为其默认值。 The default value for an int is 0. int的默认值为0。

Please note that this does not apply for local variables, eg in methods. 请注意,这并不适用于局部变量,例如在方法。 They won't be initialized automatically. 它们不会自动初始化。

public class X
{
    private int _field;

    public void PrintField()
    {
        Console.WriteLine(_field); // prints 0
    }

    public void PrintLocal()
    {
        int local;
        Console.WriteLine(local); 
        // yields compiler error "Use of unassigned local variable 'local'"
    }
}

Exactly. 究竟。 int default value is 0 . int默认值为0

是的,零是int的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM