86

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

我读过这是正确的方法:

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

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

我做错什么了?

4

11 回答 11

166

你可以这样做:

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

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

于 2012-11-14T07:16:21.610 回答
54

你可以这样做:

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

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

于 2012-11-14T07:15:38.057 回答
10

我称之为连接声明的一个例子:

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

只是我的 2 美分,希望它对某个地方的人有所帮助。

于 2014-10-10T14:23:00.397 回答
5

尝试:

 string Camnr, Klantnr, Ordernr, Bonnr, Volgnr, Omschrijving;
 Camnr = Klantnr = Ordernr = Bonnr = Volgnr = Omschrijving = string.Empty;
于 2012-11-14T07:16:53.890 回答
4

尝试

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 = "";
于 2012-11-14T07:19:51.523 回答
4
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;
于 2012-11-14T07:22:00.843 回答
4

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

var Foo = 0, Bar = 0;

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

相似地,

var Foo, Bar;

隐式类型变量必须初始化

于 2018-07-12T21:31:23.003 回答
4

现在可以通过以下方式声明和初始化多个变量:

var (anInt, aFloat, aBoolean, aChar, aString, anArray, aRecordType, anObjectType) = 
    (1, 2.14, true, 'a', "C# is awesome!", new[] { "Asia", "Europe" } , new Country { Name = "India"}, new City { Name = "Kolkata"} ); 
Console.WriteLine(anInt);
Console.WriteLine(aFloat);
Console.WriteLine(aBoolean);
Console.WriteLine(aChar);
Console.WriteLine(aString);
Array.ForEach(anArray, Console.WriteLine);
Console.WriteLine(aRecordType.Name);
Console.WriteLine(anObjectType.Name);

以下是所需自定义类型的定义:

internal record Country { internal string Name {get; set;}}
internal class City { internal string Name {get; set;}}

这已经在 .NET 5/C# 9 上进行了测试。

以下是 Linqpad 中的输出:

于 2021-05-13T09:57:09.290 回答
3

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

于 2020-01-31T11:31:56.673 回答
2

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

声明多个变量

  • 要么:用单独的值初始化它们
int i = 0, j = 1; // declare and initialize each, single-type only.
// Note: `var` is NOT supported as of C# 8.0, but see below.

// ===
// C#  7.0+ / .NET Framework 4.7+ using syntactic sugar based on *value tuples*:

// Infer the types from the RHS tuple elements.
var (i, s) = (0, "hi");

// Explicitly type the individual variables.
(int i, string s) = (0, "hi");
  • 或:用相同的值初始化它们
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;

什么不起作用

  • 除非您使用值元组语法,否则不能var在上述语句中使用,因为var仅适用于 (a) 具有初始化值的声明(可以从中推断出类型)和 (b),从 C# 8.0 开始, 如果该声明是语句中唯一的声明(否则你会得到编译错误error CS0819: Implicitly-typed variables cannot have multiple declarators)。

  • 仅在多声明语句中的最后一个变量之后放置初始化值仅初始化最后一个变量

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

于 2020-02-04T03:05:11.987 回答
0

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

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

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

于 2017-11-17T16:55:56.593 回答