I am a beginner and learning web development using ASP.Net MVC 5 framework in C# language. I came across below code:
Scenario 1: No New Keyword used in Object Creation.
[AuthorizeFunc]
[BlockWidget]
public PartialViewResult WidgetPayments()
{
PaymentFormMV data; // No New Keyword used
if (SimUtils.IsDelayedPaymentAllowed)
{
data = Pay.GetData(PaymentPageMode.DelayedPayment);
}
else
{
data = PayHelp.GetData(PaymentPageMode.MakePayment);
}
return PartialView("PaymentsWrapper", data);
}
Scenario 2: New Keyword used in Object Creation.
[AuthorizeFunc]
[BlockWidget]
public PartialViewResult WidgetPayments()
{
PaymentFormMV data = new PaymentFormMV(); // New Keyword used
if (SimUtils.IsDelayedPaymentAllowed)
{
data = Pay.GetData(PaymentPageMode.DelayedPayment);
}
else
{
data = PayHelp.GetData(PaymentPageMode.MakePayment);
}
return PartialView("PaymentsWrapper", data);
}
I am a beginner and I tried both code, and both code snippets work fine for me.
- Question 1: Why both code works fine?
- Question 2: When to use
newand when not to usenew. Can some one tell me one example for both scenario? - Question 3: What is the difference between both of them?
Can some one please guide me a little?