C#学习笔记1:属性和方法

拖延了半年时间终于再度拾起之前的C#,埋头自学起来。
跟着中文版的Sams Teach Yourself C#5.0的教程 慢慢学下来,虽然没有书上说的24小时就能成功那样神速,倒也学得不亦乐乎。
书中第4章里的属性和方法小节有道题目(p.67),一开始无论如何不知道是如何实现(去年在MVA上自学的课程基本上算还给那个老外了)。还好有Google,在我耐着性子(其实快想掀桌了(╯-_-)╯╧╧)尝试各种搜索之后,居然找到了CSDN上有人跟我看同一本书,并把实现的方法写了出来。方法并不复杂,可见我的编程素养还近乎是零啊。
代码如下
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
namespace ConsoleHelloWorld
{
class Program
{
static void Main(string[] args)
{
Contact c = new Contact();
Console.WriteLine(c.ToString());
c.FirstName = "Jim";
c.LastName = "Morrison";
c.DateOfBirth = new DateTime(1943, 12, 8);
Console.WriteLine(c.ToString());
c.Search(37.479444f, -122.450278f);
c.Search(37.479444f, -122.450278f, 50);
c.Search(37.479444f, -122.450278f, 50, "en");
Console.WriteLine(c.VerifyEmailAddress("[email protected]"));
Console.WriteLine(c.VerifyEmailAddress("[email protected]"));
}
}
}

Contact.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleHelloWorld
{
class Contact
{
private DateTime dateOfBirth;
public DateTime DateOfBirth
{
get
{
return this.dateOfBirth;
}
set
{
this.dateOfBirth = value;
}
}
public string FirstName  //自动实现的属性
{
get;
set;
}
public string LastName
{
get;
set;
}
public string FullName  //计算得到的属性
{
get
{
return FirstName + " " + LastName;
}
}
public override string ToString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("Name: {0}rn", FullName);  //使用计算得到的属性
stringBuilder.AppendFormat("Date of Birth: {0}rn", this.dateOfBirth);
return stringBuilder.ToString();
}
public bool VerifyEmailAddress(string emailAddress)
{
return emailAddress == "[email protected]";
}
public void Search(float latitude, float longitude)
{
Search(latitude, longitude, 10, "en-US");
}
public void Search(float latitude, float longitude, int distance)
{
Search(latitude, longitude, distance, "en-US");
}
public void Search(float latitude, float longitude, int distance, string culture)
{
Console.WriteLine("latitude {0} longitude {1} distance {2} culture {3}", latitude, longitude, distance, culture);
}
}
}