Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How static modifier works?

Tags:

c#

static

Suppose I have a static property like the following:

public static object MyProperty
{
   get { return new object(); }
}

Is the above property the same of?

private static object my_filed = new object();

public static object MyProperty
{
   get { return my_filed; }
}

I.e: Is the new object created each time I call my static property or only the first time?

like image 622
Nick Avatar asked Apr 01 '26 14:04

Nick


1 Answers

The property getter is executed every time the property is read, so it will create a new object instance every time.

like image 147
Guffa Avatar answered Apr 03 '26 03:04

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!