What is the difference between the two cases below:
class Data
{
PersonDataContext persons = new PersonDataContext();
public Data() {}
}
versus
class Data
{
PersonDataContext persons;
public Data()
{
persons = new PersonDataContext();
}
}
I have the same question in asp.net:
public partial class Data : System.Web.UI.Page
{
PersonDataContext persons = new PersonDataContext();
protected void Page_Load(object sender, EventArgs e)
{
}
}
versus
public partial class Data : System.Web.UI.Page
{
PersonDataContext persons;
protected void Page_Load(object sender, EventArgs e)
{
persons = new PersonDataContext();
}
}
In the first case you initialize the variable at the site of declaration.
In the second, you initialize it in a constructor.
The main difference is that if you have additional constructors and either forget to initialize or chain the constructors, you can have an uninitialized variable.
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