Ahmed Sazar Ahmed Sazar - 1 month ago 4x
C# Question

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? (See picture)

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

public string FirstName { get; set; }

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

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

enter image description here


The code LoginModel log = new LoginModel ( ) ; will create a new instance of the class LoginModel which will contains all accessible properties and fields with default values. If you want log to be null means declare like this:

LoginModel log;

Note : You should assign an instance of LoginModel class to the log to access values from it. Else it will throws NullReferenceException