简体   繁体   English

为什么我的对象不为NULL

[英]Why is my Object not NULL

I have in my program defined an object log. 我在程序中定义了一个对象日志。

LoginModel log = new LoginModel ( ) ;

I do not write values ​​into it. 我不向其中写入价值。 But why the object is not NULL when my function return it? 但是为什么当我的函数返回该对象时该对象不是NULL? (See picture) (见图片)

[Bind(Exclude = "UserID")]
public class LoginModel
{
    [ScaffoldColumn(false)]
    public int UserID { get; set; }

    [ScaffoldColumn(false)]
    public string FirstName { get; set; }

    [Display(Name = "E-mailadresse")]
    [Required(ErrorMessage = "Skriv venligst din e-mailadresse", AllowEmptyStrings = false)]
    [EmailAddress(ErrorMessage = "Ugyldig e-mailadresse")]
    [DataType(DataType.EmailAddress)]
    public string Emailaddress { get; set; }

    [Display(Name = "Kodeord")]
    [Required(ErrorMessage = "Skriv venligst et kodeord", AllowEmptyStrings = false)]
    [DataType(DataType.Password)]
    [StringLength(8, MinimumLength = 4, ErrorMessage = "Kodeordet skal mindst bestå af 4-8 karakter.")]
    public string Password { get; set; }
}

在此处输入图片说明

The code LoginModel log = new LoginModel ( ) ; 代码LoginModel log = new LoginModel ( ) ; will create a new instance of the class LoginModel which will contains all accessible properties and fields with default values. 将创建类LoginModel的新实例,该实例将包含所有可访问的属性和具有默认值的字段。 If you want log to be null means declare like this: 如果您希望log为null,则表示这样声明:

LoginModel log;

Note : You should assign an instance of LoginModel class to the log to access values from it. 注意:您应该将LoginModel类的实例分配给log以从中访问值。 Else it will throws NullReferenceException 否则将抛出NullReferenceException

When you new an object it wont be null anymore. 当您新建一个对象时,它将不再为null。

if you have 如果你有

LoginModel log;

or 要么

LoginModel log =null;

Then it will be null. 然后它将为空。 when you write 当你写

LoginModel log = new LoginModel ( ) ;

It will new your object and assign in to the variable (log),so it won't be null anymore. 它将新创建对象并分配给变量(log),因此它将不再为null。 it will be the object with properties as you have initialized in your constructor (in your case there is no initialization, so every nullable property will be null and integers will be 0 , etc) 它将是具有在构造函数中初始化的属性的对象(在您的情况下,没有初始化,因此每个可为null的属性将为null,整数将为0,等等)

https://en.wikipedia.org/wiki/Default_constructor https://en.wikipedia.org/wiki/Default_constructor

In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. 在Java和C#中,“默认构造函数”是指如果未为该类定义构造函数,则由编译器自动生成的无效构造函数。 The default constructor implicitly calls the superclass's nullary constructor, then executes an empty body. 默认构造函数隐式调用超类的null构造函数,然后执行一个空主体。 All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), false (boolean type), or null (reference types). 所有字段均保留其初始值0(整数类型),0.0(浮点类型),false(布尔类型)或null(引用类型)。 A programmer-defined constructor that takes no parameters is also called a default constructor in C# , but not in Java 程序员定义的不带参数的构造函数在C#中也称为默认构造函数,但在Java中不称为默认构造函数

And as everyone says, if you call the default constructor, the value of that object won't be null. 就像每个人都说的那样,如果调用默认构造函数,则该对象的值将不会为null。

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

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