Code runs only when a property is called. This is easy to check for yourself:
class MyClass
{
public string SqlQuery
{
get
{
Console.WriteLine("Code was run here!");
return "foo";
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Constructing object.");
MyClass myObject = new MyClass();
Console.WriteLine("Getting property.");
string sqlQuery = myObject.SqlQuery;
Console.WriteLine("Got property: " + sqlQuery);
}
}
Output:
Constructing object.
Getting property.
Code was run here!
Got property: foo
source
share