我在過去的幾個月裏一直在用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;
}
}
}
對不起節目是因爲我做這個工作有點不完整的。
您可以發佈示例代碼,而不是嘗試解釋代碼。 – deathismyfriend
你能舉兩個例子嗎?我不確定你的意思是「串聯」。你的意思是string.Format(「{0} {1}」,「bob」,「叔叔」) –
A','不用於連接。你能舉一個你認爲如何運作的例子嗎? – Enigmativity