Ahmed Sazar Ahmed Sazar - 2 months ago 14
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
{
[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; }
}


enter image description here

Answer

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