I was wondering if it is safe to create new objects as part of a method (or indeed constructor call)
Such as the convuluted example below:
public class Igloo
{
public int i = 5;
}
public class Program
{
static void Main(string[] args)
{
DoSomething(new Igloo());
}
private static void DoSomething(Igloo i)
{
Debug.WriteLine(i.i);
}
}
Is creating temporary objects like this frowned upon? Does the new object that is created get cleaned up properly.
There is nothing wrong with that.
In release mode, creating a variable for the object should end up compiling to the same IL (disclaimer: I haven't checked).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With