Skip to content

Instantly share code, notes, and snippets.

@sirchristian
Created April 5, 2014 13:57
Show Gist options
  • Save sirchristian/9992317 to your computer and use it in GitHub Desktop.
Save sirchristian/9992317 to your computer and use it in GitHub Desktop.
New C# features
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