2015-07-19 126 views
-3

我在過去的幾個月裏一直在用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; 
     } 
    } 
} 

對不起節目是因爲我做這個工作有點不完整的。

+5

您可以發佈示例代碼,而不是嘗試解釋代碼。 – deathismyfriend

+1

你能舉兩個例子嗎?我不確定你的意思是「串聯」。你的意思是string.Format(「{0} {1}」,「bob」,「叔叔」) –

+1

A','不用於連接。你能舉一個你認爲如何運作的例子嗎? – Enigmativity

回答

2

逗號用作格式化字符串時的分隔符,而加號用於正常連接。

格式是您擁有佔位符的位置,例如{0}{1}。用逗號分隔的其他字符串用於填充這些佔位符。請注意,逗號不是運算符,只是用於分隔參數。該輸出Hello world!

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

Concantenation更直截了當。它只是按順序組合字符串。同樣的例子,但與串聯:

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

編輯:現在你已經包含了特定的問題,這裏是你可以嘗試什麼:

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

我會提到','不是操作符,它只是將參數分隔到[Console.WriteLine](https://msdn.microsoft.com/en-us/library/828t9b9h(v = vs.110)的.aspx) – Zantier

0

+是「加法運算」。對於數字,它會添加它們。對於日期和時間跨度,它添加它們。對於字符串(文本) - 它將它們連接在一起,因爲幾乎沒有任何其他的將文本「添加」到文本中的意義。

此外,+能夠「添加」/「連接」/「連接」非文本到文本,所以"mom" + 5實際上會產生"mom5"文本。但這是一個破解。它在這種情況下什麼,居然是:

"mom" + (5).ToString() 

所以它叫「的ToString()」的整數5,得到一個字符串爲「5」的結果,並添加/連接兩個文本在一起。通過這種方式,它可以將任何東西「添加」到文本中 - 它只是對所有內容進行「抽取」,並在之後加入字符串。現在

,第二件事:

逗號從來沒有像拼接工作。期。

逗號用於將參數傳遞給函數/方法。 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。它以同樣的方式工作:採用一種格式,一些參數,生成一個結果並將其作爲字符串返回,因此除了將其轉儲到控制檯之外,您可以對其執行某些操作。

檢查。考慮這一點,並認爲如何只用「+」無formatstrings寫它:

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。請隨時嘗試一下。

相關問題