0

which is the best memory efficient/efficient way to declare string and other data type variables in c#!?

Option 1

string strAssociateId = context.Request.QueryString["xxxxxx"], 
                strAssociateName = context.Request.QueryString["xxxxx"],
                strPhoto = context.Request.QueryString["xxxxx"],
                strDescription = context.Request.QueryString["xxxxx"];

or

Option 2

string strAssociateId = context.Request.QueryString["xxxxx"];
string strAssociateName = context.Request.QueryString["xxxxx"];
string strPhoto = context.Request.QueryString["xxxxx"];
string strDescription = context.Request.QueryString["xxxxx"];

or any other way!?

which is the best way to follow on a longer run?! or both have the same efficiency!?

downvoters pls comment so that i can correct.

I am just trying to find the best way and this question is not there in stackoverflow before!!

this will not lead to any discussion, and the question is completely answerable and clear

Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
  • 2
    call me crazy, but I'd just use MVC - then this is just parameters on a method: `public ActionResult SomeMethod(string associateId, string associateName, ...)` - where the parameter names match the "xxxxx" if you see what I mean; mapping query string parameters to variables is not a good use of your time – Marc Gravell Aug 16 '13 at 10:45
  • 1
    I would argue that premature optimization (or just, mindless optimization) is the devil of any development............ – Matías Fidemraizer Aug 16 '13 at 10:46
  • @MarcGravell I am using ajax call and passing these value as querystring[data:{foo:foo}]!! can I do it in any other way so that it will stay hidden!? – Vignesh Subramanian Aug 16 '13 at 10:51
  • 1
    @vignesh it would depend on the exact requirements, but I'll default to "yes, probably" – Marc Gravell Aug 16 '13 at 11:06
  • downvoters pls comment so that i can correct. I have got two downvotes for this question, I am just trying to find the best way and this question is not there in stackoverflow before!! this will not lead to any discussion, and the question is completely answerable and clear. – Vignesh Subramanian Aug 16 '13 at 11:08
  • http://stackoverflow.com/questions/9109022/what-is-difference-between-single-and-multiple-declarations-in-one-line – Marc Gravell Aug 16 '13 at 11:13
  • thats asking for the difference and this asks for better way/comparison! People tend to google "what is the best way to declare" rather than "what is the difference between single line and multiline declaration" This might reach people faster and help them as the question is explicit – Vignesh Subramanian Aug 16 '13 at 11:21

4 Answers4

11

The only difference between those options, is readability. There are no performance difference, and they will generate the exact same IL.

For readability, I would choose option 2.

driis
  • 161,458
  • 45
  • 265
  • 341
3

They will be compiled to the same IL, your Option 1 is simply syntactic sugar.

I my opinion Option 2 is better because it has better readability.

juhan_h
  • 3,965
  • 4
  • 29
  • 35
1

which is the best way to follow on a longer run

Both are best as both will generate the same IL.

or both have the same efficiency

Yes as both have same efficiency.

Which one to follow

You need to follow the one that is followed through out your application to support consistency. However if you are starting a new project you can decide not which one you like the most. My preference is the second one as it is more common as well as more readable

Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

Both are better. But you are gonna mess up with the code. If you prefer using second method you might require or consider using comments

// this code is this..

So that you can know what variable or string was written here. Either you might think that this is a parameter to something.

The first method is more lovey to everybody. However I don't use strings. I use simple vars.

To follow a long run, you can use any of them. They don't have any time consuming effect. To check more about these You can use IE F12 Developer Tools. To Test which page is using more time to get loaded.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • "The first method is more lovey to everybody." since several people in this thread are expressing a preference to the second version, this is demonstrably not true; what you mean is "to me"... "Either you might think that this is a parameter to something." - er... why? And it is not clear what that comment would add that isn't already trivially visible in the code – Marc Gravell Aug 16 '13 at 10:49
  • 1
    No actually. when writing long strings like this. You might need to add a slight comment in there about its description. And I did add "to me" to make it clear that I prefer this one. – Afzaal Ahmad Zeeshan Aug 16 '13 at 10:54
  • Comment is just to tell yourself that this variable or string is used to this job. Not to make any change in the code! – Afzaal Ahmad Zeeshan Aug 16 '13 at 10:54
  • "Comment is just to tell yourself that this variable or string is used to this job." - if you need a comment to tell you the job of a variable, then you've named it wrong. And there is not much readability difference between the two layouts; "long strings like this" - meh; there is nothing unclear in either – Marc Gravell Aug 16 '13 at 11:05
  • 1
    I prefer using comments for long threads of codes. To get to where I want to go. If you don't like doing so skip it! :) – Afzaal Ahmad Zeeshan Aug 16 '13 at 11:11