Created
April 5, 2014 13:57
-
-
Save sirchristian/9992317 to your computer and use it in GitHub Desktop.
New C# features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Console; | |
namespace RoslynTests | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var chris = new Person("Chris", "20"); | |
chris.Details = new Dictionary<string, string>() {["job"] = "software engineer" }; | |
WriteLine(chris.SayHi()); | |
} | |
} | |
public class Person(string name, string age) | |
{ | |
public string Name { get; } = name; | |
private string _age = age; | |
public Dictionary<string, string> Details { get; set; } = null; | |
public string SayHi() | |
{ | |
var greeting = new StringBuilder(); | |
greeting.AppendFormat("Hello {0}, ", Name); | |
if (int.TryParse(_age, out int a)) | |
greeting.AppendFormat("I see you are {0} years old.", _age); | |
if (Details != null && Details.Count > 0) | |
{ | |
greeting.AppendLine(); | |
greeting.AppendLine("DETAILS:"); | |
foreach (var detail in Details) | |
greeting.AppendLine(string.Format("{0} = {1}", detail.Key, detail.Value)); | |
} | |
return greeting.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment