Click here to Skip to main content
15,908,015 members
Home / Discussions / C#
   

C#

 
QuestionWhich Constructor is Better? Pin
Jasmine250116-Apr-13 12:15
Jasmine250116-Apr-13 12:15 
This is kind of for my own entertainment, but I was about to say "the second thing is better" to someone, but now that I think about it I'm not sure...

Option 1
C#
public class MyClass {
  private System.String SettingValue = ""; 
  private System.String SETTING_KEY = "SomeSettingKey"; 


  public MyClass() {
  	this.Server = System.Configuration.ConfigurationManager.AppSettings[this.SETTING_KEY];
  }
}


Option 2
C#
public class MyClass {
    private System.String SettingValue = 
        System.Configuration.ConfigurationManager.AppSettings["SomeSettingKey"];


    public MyClass() { }
}


My reasoning is that the setting key is only ever used in one place so creating a constant is unnecessary overhead, and secondly, there is no reason to create an empty string, then discard that string to the garbage heap, then set it to the value you really wanted... just set it to the correct value on instantiation.
AnswerRe: Which Constructor is Better? Pin
Matt T Heffron16-Apr-13 12:47
professionalMatt T Heffron16-Apr-13 12:47 
GeneralRe: Which Constructor is Better? Pin
Jasmine250116-Apr-13 14:34
Jasmine250116-Apr-13 14:34 
GeneralRe: Which Constructor is Better? Pin
PIEBALDconsult16-Apr-13 15:01
mvePIEBALDconsult16-Apr-13 15:01 
GeneralRe: Which Constructor is Better? Pin
Jasmine250117-Apr-13 5:36
Jasmine250117-Apr-13 5:36 
GeneralRe: Which Constructor is Better? Pin
Matt T Heffron17-Apr-13 9:53
professionalMatt T Heffron17-Apr-13 9:53 
GeneralRe: Which Constructor is Better? Pin
Jasmine250117-Apr-13 9:57
Jasmine250117-Apr-13 9:57 
GeneralRe: Which Constructor is Better? Pin
Matt T Heffron17-Apr-13 10:02
professionalMatt T Heffron17-Apr-13 10:02 
GeneralRe: Which Constructor is Better? Pin
Jasmine250117-Apr-13 10:04
Jasmine250117-Apr-13 10:04 
AnswerRe: Which Constructor is Better? Pin
Matt T Heffron17-Apr-13 10:07
professionalMatt T Heffron17-Apr-13 10:07 
GeneralRe: Which Constructor is Better? Pin
Matt T Heffron17-Apr-13 13:35
professionalMatt T Heffron17-Apr-13 13:35 
AnswerRe: Which Constructor is Better? Pin
V.16-Apr-13 20:23
professionalV.16-Apr-13 20:23 
AnswerRe: Which Constructor is Better? Pin
Orjan Westin16-Apr-13 22:45
professionalOrjan Westin16-Apr-13 22:45 
GeneralRe: Which Constructor is Better? Pin
Richard Deeming17-Apr-13 0:39
mveRichard Deeming17-Apr-13 0:39 
GeneralRe: Which Constructor is Better? Pin
Orjan Westin17-Apr-13 1:58
professionalOrjan Westin17-Apr-13 1:58 
GeneralRe: Which Constructor is Better? Pin
Jasmine250117-Apr-13 5:39
Jasmine250117-Apr-13 5:39 
AnswerRe: Which Constructor is Better? Pin
jschell17-Apr-13 9:18
jschell17-Apr-13 9:18 
GeneralRe: Which Constructor is Better? Pin
Jasmine250117-Apr-13 9:28
Jasmine250117-Apr-13 9:28 
QuestionLoading Very Large DataSet Without losing any information Pin
losan16-Apr-13 7:23
losan16-Apr-13 7:23 
AnswerRe: Loading Very Large DataSet Without losing any information Pin
Dave Kreskowiak16-Apr-13 7:58
mveDave Kreskowiak16-Apr-13 7:58 
AnswerRe: Loading Very Large DataSet Without losing any information Pin
SledgeHammer0116-Apr-13 8:05
SledgeHammer0116-Apr-13 8:05 
AnswerRe: Loading Very Large DataSet Without losing any information Pin
Pete O'Hanlon16-Apr-13 9:33
mvePete O'Hanlon16-Apr-13 9:33 
GeneralRe: Loading Very Large DataSet Without losing any information Pin
Rockstar_16-Apr-13 18:24
professionalRockstar_16-Apr-13 18:24 
AnswerRe: Loading Very Large DataSet Without losing any information Pin
Mycroft Holmes16-Apr-13 17:23
professionalMycroft Holmes16-Apr-13 17:23 
AnswerRe: Loading Very Large DataSet Without losing any information Pin
V.16-Apr-13 22:09
professionalV.16-Apr-13 22:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.