连接时,和+的区别是什么?

本文关键字:区别 是什么 连接 | 更新日期: 2023-09-27 18:03:24

我已经用c#编码了几个月了,但是每次我连接时,我总是混淆逗号,和加号+之间的区别。有时,用于连接,有时+用于连接。我真的不明白其中的区别。请帮帮我。

代码如下......

class Faculty
{
    string firstName, lastName, address, dateOfBirth, phoneNO, fathersName, mothersName;
    public string FirstName
    {
        get { return firstName; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Enter a valid first name");
            else
                firstName = value;
        }
    }
    public string LastName
    {
        get { return lastName; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Enter a valid last name");
            else
                lastName = value;
        }
    }
    public string Address
    {
        get { return this.address; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Enter a valid adddress");
            else
                address = value;
        }
    }
    public string DateOfBirth
    {
        get { return this.dateOfBirth; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Enter a valid date of birth");
            else
                dateOfBirth = value;
        }
    }
    public string PhoneNo
    {
        get { return this.phoneNO; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Enter a valid phone no.");
            else
                phoneNO = value;
        }
    }
    public string FathersName
    {
        get { return this.fathersName; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Enter a valid name");
            else
                fathersName = value;
        }
    }
    public string MothersName
    {
        get { return this.mothersName; }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new Exception("Enter a valid name");
            else
                mothersName = value;
        }
    }
}
class ExaminationCentre
{
    Dictionary<int, Faculty> dict = new Dictionary<int, Faculty>();
    Faculty counsellor; int id = 100; DateTime yearOfBirth;
    public void AddMembers()
    {
        counsellor = new Faculty(); ;
        Console.Write("Enter your first name: ");
        counsellor.FirstName = Console.ReadLine();
        Console.Write("Enter your last name: ");
        counsellor.LastName = Console.ReadLine();
        Console.Write("Enter your date of birth: ");
        counsellor.DateOfBirth = Console.ReadLine();
        yearOfBirth = DateTime.Parse(counsellor.DateOfBirth);
        int age = DateTime.Now.Year - yearOfBirth.Year;
        Console.Write("Enter your father's name: ");
        counsellor.FathersName = Console.ReadLine();
        Console.Write("Enter your mother's name: ");
        counsellor.MothersName = Console.ReadLine();
        Console.Write("Enter your phone no: ");
        counsellor.PhoneNo = Console.ReadLine();
        showID();
        dict.Add(id, counsellor);
    }
    void showID()
    {
        Console.WriteLine("Your id is: " , id++);//but when I'm doing Console.WriteLine("Your id is:"+id++); it isshowing the id.
    }
}
class Program
{
    static void Main(string[] args)
    {
        ExaminationCentre centre;
        Console.WriteLine("Menu");
        Console.WriteLine("1.Add");
        Console.WriteLine("2.Update");
        Console.WriteLine("3.Delete");
        Console.WriteLine("4.Search");
        int select = int.Parse(Console.ReadLine());
        switch (select)
        {
            case 1:
                centre = new ExaminationCentre();
                centre.AddMembers();
                break;
        }
    }
}

对不起,程序有点不完整,因为我正在处理它。

连接时,和+的区别是什么?

逗号在格式化字符串时用作分隔符,而加号用于正常的连接。

格式化是你有{0}{1}等占位符的地方。用逗号分隔的其他字符串用于填充这些占位符。注意,逗号不是操作符,只是用来分隔参数。输出Hello world!:

string greet = "Hello";
Console.WriteLine("{0}{1}", greet, " world!");

浓缩更直接。它只是按顺序组合字符串。相同的示例,但使用了连接:

string greet = "Hello";
Console.WriteLine(greet + " world!");

编辑:现在你已经包含了具体的问题,这里是你可以尝试的:

Console.WriteLine("Your id is: {0}" , id++);

+为"加法运算符"。对于数字,它将它们相加。对于日期和时间跨度,它会添加它们。对于字符串(文本)-它将它们连接起来,因为几乎没有其他意义上的"添加"一个文本到一个文本。

此外,+能够将非文本"添加"/"连接"/"连接"到文本中,因此"mom" + 5实际上会生成"mom5"文本。但这是一个黑客。在这个例子中,它实际上是:

"mom" + (5).ToString()

所以它在整数5上调用"ToString()",得到一个字符串"5"作为结果,并将两个文本添加/连接在一起。这样,它就可以在文本中"添加"任何东西——它只是"tostring"的一切,然后将字符串连接起来。

现在,第二件事:

逗号不能用作连接。时期。

逗号用于向函数/方法传递参数。WriteLine是一个方法,它接受一些参数。当你通过编写method( )来调用方法时,你可以在( )中传递参数,即method( 5 )method( "foo" )。如果有多个参数,那么它们必须用,(逗号)隔开:method( 5, "foo", "bar"),并且并不意味着 5, foo和bar将被连接起来。它们会被传递给这个方法,这个方法会…

WriteLine函数只能将东西连接起来。你可以在MSDN或其他文档上读到它的具体功能。简而言之,它接受所谓的"格式字符串"和一些额外的参数,然后将这些额外的参数粘合到"格式字符串"中以构建结果。例如:

format:  Hello {0}!
param1:  Ted
code:    WriteLine("Hello {0}!", "Ted");
result:  Hello Ted!
format:  Hello {0}!
param1:  12.36
code:    WriteLine("Hello {0}!", 12.36);
result:  Hello 12.36!
format:  Hello {0} and {1}!
param1:  John
param2:  Bob
code:    WriteLine("Hello {0} and {1}!", "John", "Bob");
result:  Hello John and Bob!
code:    WriteLine("I ate {0} {1} {2}!", 5, "bad", "bananas");
result:  I ate 5 bad bananas!

{0}{1}等标记是…标记。它们表示将输入其中一个参数的位置。这种标记的通用名称是"占位符"。

WriteLine将文本直接写入控制台。一个更有用的类似函数是string.Format。它的工作方式是一样的:接受一个格式,一些参数,构建一个结果,并将其作为字符串返回,这样你就可以对它做一些事情,而不仅仅是将它转储到控制台。

检查。考虑一下这一点,并考虑如何使用"+"而不使用formatstring:

WriteLine("Make me a Foo + {0} bars of {1}", 4 + 2, "goo");

剧透:

method: WriteLine
number of params: 3
param1: "Make me a Foo + {0} bars of {1}"   (formatstring)
params: 4 + 2                               (== 6, goes as {0})
param3: "goo"                               (goes as {1})
result: Make me a Foo + 6 bars of goo
alternative way to get such text with extra parens for easier reading:
    ("Make me a Foo + ")  +  (4+2)  +  ("bars of ")  +  ("goo")

因此,另一种方法是将该公式作为一个大参数作为一个整体传递给WriteLine——总而言之,该公式生成一个可使用的文本,我们只需要显示它,不需要格式化:

WriteLine( ("Make me a Foo + ")  +  (4+2)  +  ("bars of ")  +  ("goo") );
// or
WriteLine( "Make me a Foo + "  +  (4+2)  +  "bars of "  +  "goo" );

很难读,一个临时变量可能会有所帮助:

string text = "Make me a Foo + "  +  (4+2)  +  "bars of "  + "goo";
WriteLine( text );

这只是WriteLine现在的一个参数。这种情况下不需要逗号。

注意,4+2必须在括号中,否则它会被粘合到字符串中,即4然后2,而不是4+2。你可以试试

每个人都告诉你,,从来不连接。但它用于将形参传递给执行连接的函数。

我从我的一个开发人员那里得到了同样的问题。经过一番讨论,我意识到他认为,在调用函数之前进行了连接,并且只传递了一个字符串作为参数。他这样想只是因为他不知道可以构建接受可变数量参数的函数。

这里你可以看到我是如何使用params关键字来告诉c#应该接受可变数量的字符串参数的。这就是Console.WriteLine()实际在做的

 // ConcatenateStrings("multiple", "strings", "are", "passed", "as", "parameters");
 public string ConcatenateStrings(string firstString, params string[] restOfTheStrings)
 {
     // This code is an example only. It is not optimal
     foreach(string oneString in restOfTheStrings)
     {
         firstString += oneString;
     }
     return firstString;
}