同时声明并分配多个字符串变量

时间:2012-11-14 07:13:17

标签: c# string

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

我读过这是正确的方法:

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

我做错了什么?

11 个答案:

答案 0 :(得分:116)

你可以这样做:

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

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

答案 1 :(得分:34)

你可以这样做:

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

或者您可以先将它们全部声明,然后在下一行中使用您的方式。

答案 2 :(得分:6)

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

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

只需2美分,希望它可以帮到某个人。

答案 3 :(得分:5)

尝试:

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

答案 4 :(得分: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 = "";

答案 5 :(得分:3)

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;

答案 6 :(得分:2)

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

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 上测试过。

Following is the output in Linqpad:

答案 7 :(得分:1)

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

答案 8 :(得分:1)

所有信息都在现有答案中,但是我个人希望提供一个简洁的摘要,因此请尝试一下;这些命令使用int变量来简化,但是它们类似地适用于任何类型,包括string

声明多个变量

  • 其中之一:初始化它们每个
int i = 0, j = 1; // declare and initialize each; `var` is NOT supported as of C# 8.0
  • 或:将它们全部初始化为相同的值
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)。

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

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

答案 9 :(得分:0)

相当陈旧的问题,但是有人回去 这不像上面的其他答案一样紧凑,但使用Visual Studio多行选择快捷键[Alt + Shift +↑](或其他方向)可以更容易阅读和输入

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

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

答案 10 :(得分:0)

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

var Foo = 0, Bar = 0;
  

类型简单的变量不能有多个声明符

类似地,

var Foo, Bar;
  

类型明确的变量必须初始化