Alex Rivera | Logout

C# - Downside to Setting Initial Value in Declaration

Asked 2010-06-08T14:36:07.110
9

Is there any downside to a class like:

class Example1
{
  protected string UserId = (string)Session["user"];
}
//versus

class Example2
{
  protected string UserId;
  public Example2()
  {
      UserId = (string)Session["user"];
  }
}

If I always want to set this value is there any downside to Example1?

UPDATE:
Session["user"] is set in the Global.asax Session_Start. So if this fails. Nothing should work anyways.

Edit
Report

1 Answer

0

You might someday want a second constructor with a different value of UserId.

answered 2010-06-08T16:23:56.993

Your Answer