C# 同时声明和分配多个字符串变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13374454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:21:17  来源:igfitidea点击:

Declare and assign multiple string variables at the same time

c#variable-declarationvariable-initialization

提问by Mathlight

I'm declaring some strings that are empty, so it won't throw errors later on.

我声明了一些空字符串,因此以后不会抛出错误。

I've read that this was the proper way:

我读过这是正确的方法:

string Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = Startdatum = Bonprioriteit = Matsoort = Dikte = Draaibaarheid = Draaiomschrijving = Orderleverdatum = Regeltaakkode = Gebruiksvoorkeur = Regelcamprog = Regeltijd = Orderrelease = "";

But that doesn't work. I get this error: Klantnr does not exist in the current context.

但这不起作用。我收到此错误:Klantnr does not exist in the current context

What did I do wrong?

我做错了什么?

采纳答案by Habib

You can do it like:

你可以这样做:

string Camnr, Klantnr, Ordernr, Bonnr, Volgnr;// and so on.
Camnr = Klantnr = Ordernr = Bonnr = Volgnr = string.Empty;

First you have to define the variables and then you can usethem.

首先,您必须定义变量,然后才能使用它们。

回答by Botz3000

You can to do it this way:

你可以这样做:

string Camnr = "", Klantnr = "", ... // or String.Empty

Or you could declare them all first and then in the next line use your way.

或者你可以先声明它们,然后在下一行使用你的方式。

回答by Carlos Landeras

Try with:

尝试:

 string Camnr, Klantnr, Ordernr, Bonnr, Volgnr, Omschrijving;
 Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = string.Empty;

回答by sajanyamaha

Try

尝试

string     Camnr , Klantnr , Ordernr , Bonnr , Volgnr , Omschrijving , Startdatum ,    Bonprioriteit , Matsoort , Dikte , Draaibaarheid , Draaiomschrijving , Orderleverdatum , Regeltaakkode , Gebruiksvoorkeur , Regelcamprog , Regeltijd , Orderrelease ;

and then

进而

Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = Startdatum = Bonprioriteit = Matsoort = Dikte = Draaibaarheid = Draaiomschrijving = Orderleverdatum = Regeltaakkode = Gebruiksvoorkeur = Regelcamprog = Regeltijd = Orderrelease = "";

回答by Kishore Kumar

string Camnr , Klantnr , Ordernr , Bonnr , Volgnr , Omschrijving , Startdatum , Bonprioriteit , Matsoort , Dikte , Draaibaarheid , Draaiomschrijving , Orderleverdatum , Regeltaakkode , Gebruiksvoorkeur , Regelcamprog , Regeltijd , Orderrelease;
Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = Startdatum = Bonprioriteit = Matsoort = Dikte = Draaibaarheid = Draaiomschrijving = Orderleverdatum = Regeltaakkode = Gebruiksvoorkeur = Regelcamprog = Regeltijd = Orderrelease = string.Empty;

回答by HowlinWulf

An example of what I call Concatenated-declarations:

我称之为串联声明的一个例子:

string Camnr = "",
        Klantnr = "",
        Ordernr = "",
        Bonnr = "",
        Volgnr = "",
        Omschrijving = "",
        Startdatum = "",
        Bonprioriteit = "",
        Matsoort = "",
        Dikte = "",
        Draaibaarheid = "",
        Draaiomschrijving = "",
        Orderleverdatum = "",
        Regeltaakkode = "",
        Gebruiksvoorkeur = "",
        Regelcamprog = "",
        Regeltijd = "",
        Orderrelease = "";

Just my 2 cents, hope it helps someone somewhere.

只是我的 2 美分,希望它可以帮助某个地方的人。

回答by G .

Fairly old question but incase someone goes back.
This isn't as compact as the other answers above, but fairly readable and easier to type using Visual Studio Multi-Line selection shortcut [Alt+ Shift + ↑] (or other directions)

相当古老的问题,但以防万一有人回去。
这不像上面的其他答案那么紧凑,但使用 Visual Studio 多行选择快捷方式 [Alt+ Shift + ↑](或其他方向)相当可读且更易于输入

string Camnr = string.Empty;
string Klantnr = string.Empty;

Type out all variable names on new lines. Multi-Select in front of them an type "string". Multi-Select behind them and type "= string.Empty;".

在新行中输入所有变量名称。多选他们前面的一个类型“字符串”。在它们后面多选并输入“= string.Empty;”。

回答by themefield

Just a reminder: Implicit type varin multiple declaration is not allowed.There might be the following compilation errors.

提醒一下:不允许在多个声明中使用隐式类型var可能存在以下编译错误。

var Foo = 0, Bar = 0;

Implicitly-typed variables cannot have multiple declarators

隐式类型的变量不能有多个声明符

Similarly,

相似地,

var Foo, Bar;

Implicitly-typed variables must be initialized

必须初始化隐式类型的变量

回答by aac

string a = "", b = a , c = a, d = a, e = a, f =a;

string a = "", b = a , c = a, d = a, e = a, f =a;

回答by mklement0

All the information is in the existing answers, but I personally wished for a concise summary, so here's an attempt at it; the commands use intvariables for brevity, but they apply analogously to any type, including string.

所有信息都在现有答案中,但我个人希望有一个简洁的总结,所以这里尝试一下;int为简洁起见,这些命令使用变量,但它们类似地适用于任何类型,包括string.

To declare multiple variables and:

声明多个变量

  • either: initialize them each:
  • 或者:初始化它们每个
int i = 0, j = 1; // declare and initialize each; `var` is NOT supported as of C# 8.0
  • or: initialize them all to the same value:
  • 或:将它们全部初始化为相同的值
int i, j;    // *declare* first (`var` is NOT supported)
i = j = 42;  // then *initialize* 

// Single-statement alternative that is perhaps visually less obvious:
// Initialize the first variable with the desired value, then use 
// the first variable to initialize the remaining ones.
int i = 42, j = i, k = i;


What doesn'twork:

什么不起作用

  • You cannot use varin the above statements, because varonly works with (a) a declaration that has an initialization value (from which the type can be inferred), and (b), as of C# 8.0, if that declaration is the onlyone in the statement (otherwise you'll get compilation error error CS0819: Implicitly-typed variables cannot have multiple declarators).

  • Placing an initialization value only after the lastvariable in a multiple-declarations statement initializes the last variable only:

    int i, j = 1;// initializes *only* j

  • 不能使用var在上面的语句,因为var只有(一)有一个初始化值(该类型可以推断)的声明,和(b)的作品,如C#8.0中,如果该声明是唯一一个在语句(否则你会得到编译错误error CS0819: Implicitly-typed variables cannot have multiple declarators)。

  • 配售后,才初始化值最后在多重声明语句变量初始化只有最后一个变量

    int i, j = 1;// initializes *only* j