Is there any difference between these two cases in below program?
static void Main(string[] args)
{
//Case 1:
new Program().a();
//Case 2:
Program p = new Program();
p.a();
}
void a()
{
// Do some stuff
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. You’re creating an object when you call
new Program().pis just a reference to that object, it adds almost nothing at all in terms of memory use or performances.In terms of style and readability of your code, it is advisable to avoid statements like
new Program().a();– It makes the code harder to debug because you can’t place a brake-point on the statement you want and can’t tell what caused an exception you may have caused.It is also not too clear what you’re doing – you’d probably need to read that again to fully understand what is created, and what is executed and returned.