When to use the NEW keyword in C # when creating an object in ASP.NET MVC 5

I am starting and studying web development using ASP.Net MVC 5 in C #. I came across the code below:

Scenrio 1: No New Keyword used when creating objects.

[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); } 

Scenrio 2: New Keyword used when creating objects.

 [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 starting and I have tried both codes, and both codes work fine for me.

Question 1: Why do both codes work fine?

Question 2: When to use the β€œnew” and when not to β€œuse”. Can someone tell me one example for both scenarios.

Question 3: What is the difference between the two.

Can someone please guide me a little.

+5
source share
3 answers

Why are both codes working fine? What is the difference between the two?

The easiest way to do this is "because they do the same." More specifically, your second piece of code does a job that is ignored; besides this one assignment, the code is identical.

When to use the β€œnew” and when not to use

When all branches of the code do the job, as in your case, do not use new . When you need to assign an object and then reassign it later, use new . If in some cases you do not need an object, but you need it in others, use null instead of new .

Note that if you leave the local variable unassigned, the compiler will check if all branches complete the task before the first reading of the variable:

 PaymentFormMV data; if (SimUtils.IsDelayedPaymentAllowed) { data = Pay.GetData(PaymentPageMode.DelayedPayment); } // No "else" return PartialView("PaymentsWrapper", data); // Compile-time error 
+3
source

Look not only at one line, look at what this method does. In all logical cases ( if and else ) this variable is set to something. (Assuming that .GetData() methods successfully return something.)

In general, you use new when you want to create a new instance of an object. The example you are showing does not need to be done because the next thing it does is replace this instance with another instance. There is no need to create something to immediately throw it away.

+5
source

In both cases, you create a new instance of the object inside your if statement so that it is configured and the assigned instance does not initially matter, since it is replaced.

If you were to request an object immediately after the declaration in the first instance, you will see that it is null, whereas in the second case it will be PaymentsFormMV.

+1
source

Source: https://habr.com/ru/post/1272018/


All Articles