0

I am supporting two different languages at my web application. So I have 2 different string for everything. I am asking that what is the best way of defining static texts. I mean are there any performance difference between these 4 four examples or any other better way?

static private string srLang1 = "Username";
private string srLang2 = "Username";
static string srLang3 = "Username";
string srLang4 = "Username";
const string srLang5="Username";
private const string srLang6 = "Username";
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • Why are you not using resource files? They are made for localizing / translating web applications. They may not be the fastest way to resolve this, but it is certainly the most scalable solution in the future. – Jonas Sep 09 '11 at 13:14
  • updated question with more variables which one is best performance ? strings won't change in any way – Furkan Gözükara Sep 09 '11 at 13:14
  • Jonas what do you mean can you explain ? – Furkan Gözükara Sep 09 '11 at 13:14
  • The following link should explain the concept of resource files: http://msdn.microsoft.com/en-us/library/ms228208.aspx. Have a read, maybe it's what you're looking for :-) – Jonas Sep 09 '11 at 13:17
  • I would suggest you go with Jonas' advice and learn about ASP.NET localization. Too be honest, your concern with performance in this specific area is very premature, it is unlikely that this will be the cause of any performance problem you face. In all cases you will be referencing the strings by there reference even in the case of the `const` so if there is any performance difference it is going to be insignificant. When it comes to localization maintainability should be your first concern and work from there, ASP.NET localization is a step in the right direction. – Chris Taylor Sep 09 '11 at 13:21

5 Answers5

4

I can't see any performance issues associated with the declarations.

Better way to provide different text for different languages would be globalizing your application using Resources.

Globalization Architecture for ASP.NET

Basically you define strings and other localizable items, such as images, in Resources. Then you can reference resources from your source code.

George Polevoy
  • 7,450
  • 3
  • 36
  • 61
1

private const string srLang6 = "Username"; seems the best. ReSharper plugin would suggest this way.

Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174
  • thanks for answer does this product definitely worth to buy ? ReSharper 6.0 Full Edition – Furkan Gözükara Sep 09 '11 at 13:20
  • It helps me to always use "vars", in cases like this question is about, and with class templates I often use. But there is really much more to it. A lot more. A "killer" combination is ReSharper + PluralSight.net video lessons on how to use it. By learning thous you would defiantly benefit greatly from it. – Maxim V. Pavlov Sep 09 '11 at 13:32
1

I suppose you are looking for Resources Files and Globalization. Search on the internet or look at the links provided in this question.

Although I would not recommend your way of globalizing an application the best choice in terms of performance between your options is const string myString = "hello" (please note that there is no difference between private type varName = something and type varName = something. In C# all members are implicitly private by default unless you specify a modifier)

as-cii
  • 12,819
  • 4
  • 41
  • 43
1

You definitely should take a look at the MSDN Article series on Globalization and Localization. It shows you how to properly support multiple languages on your site without having to do things like you try above:

ASP.NET Globalization and Localization

Start with all of the articles in the Localizing ASP.NET Web Pages By Using Resources and just keep working your way through.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

This older article talks a little about measuring performance on static vs non-static. As for private vs non-private, Everything I know says it shouldn't make a difference, but compilers sometimes do funny things.

The best answer is for you to measure the differences in your program using some sort of performance testing. As much as we can talk about theory, nothing beats cold hard data.

Community
  • 1
  • 1
Tin Can
  • 2,540
  • 2
  • 29
  • 39