Vian Ojeda Garcia Vian Ojeda Garcia - 1 year ago 35
C# Question

C# what is the difference between new 'Classname' and default('classname')

I am new to C# and not very familiar with it. I am confused with the difference of

new PostpaidProfile();
and
default(AutoliftResult);
What I mean is what is the difference on how they are call. Below is the classes or object I don't know too what they are called

public class PostpaidProfile
{

public bool WasRetrieved { get; set; }

public string AccountCategory { get; set; }

public string AccountNum { get; set; }

public string Acd { get; set; }

public string ActivationDate { get; set; }

public int? AgingDays { get; set; }

public decimal? CreditRating { get; set; }

public string CutOff { get; set; }

public string Cycle { get; set; }

public bool? IsBlacklisted { get; set; }

public bool? IsNopsa { get; set; }

public decimal? Msf { get; set; }

public string RatePlan { get; set; }

public string ServiceStatus { get; set; }

public int? VipCode { get; set; }

public string Zip { get; set; }

public string Remarks { get; set; }

}

public class AutoliftResult
{

public bool IsSuccess { get; set; }

public decimal StatusCode { get; set; }

public string Message { get; set; }

public string SRNumber { get; set; }

}


and here how they are called

PostpaidProfile output = new PostpaidProfile();

AutoliftResult output = default(AutoliftResult);


My question is what are their difference? (im not talking about their content) is it the same if I declare
AutoliftResult output = new AutoliftResult();

LB2 LB2
Answer Source

new PostpaidProfile() creates a new instance of the class.

default(AutoliftResult) creates a default value for the specified type. For reference types, it is null. For value types, it is usually whatever 0 translates to for the type - i.e. if type is int, default value is 0; if type is bool, default value is false, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download