C# 6.0
a nawet 7.0
Cezary Walenciuk
Cezary Walenciuk
public class Person
{
public int Height { get; set; }
public int Weight { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
string r = string.Format("{0} lat {1} {2} cm"
, Age, Name, Height);
return "\{Age} lat \{Name} \{Height} cm";
}
}
public static void Fun() { string hour = "\{DateTime.Now : hh}"; }
![]()
public class Person
{
public int Height { get; set; }
public int Weight { get; set; }
public int BMI => Height / Weight;
}
public class Point3D
{
public int X => 15;
public int Y => 22;
public int Z => 33;
public double Distance => Math.Sqrt(X*X + Y*Y + Z*Z);
}
Dictionary<string, Person> _persons =
new Dictionary<string, Person>()
{
{ "programista", new Person() },
{ "grafik", new Person() }
}
Dictionary<string, Person> _persons2 =
new Dictionary<string, Person>()
{
["programista"] = new Person(),
["grafik"] = new Person()
};
Dictionary<string, Person> _persons =
new Dictionary<string, Person>();
_persons.Add("programista", new Person());
_persons.Add("grafik", new Person());
Dictionary<string, Person> _persons =
new Dictionary<string, Person>();
_persons["programista"] = new Person());
_persons["grafik"] = new Person());
public Newtonsoft.Json.Linq.JObject ToJson()
{
var result = new Newtonsoft.Json.Linq.JObject();
result["height"] = Height;
result["weight"] = Weight;
result["age"] = Age;
result["name"] = Name;
return result;
}
public Newtonsoft.Json.Linq.JObject ToJson2()
{
var result = new Newtonsoft.Json.Linq.JObject()
{
["height"] = Height,
["weight"] = Weight,
["age"] = Age,
["name"] = Name
};
return result;
}
using System;
namespace CSharp6Example
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
}
using System.Console;
namespace CSharp6Example
{
class Program
{
static void Main(string[] args)
{
WriteLine("Hello World");
}
}
}
public static class StringExtension
{
public static string JoinAndAddSeperator(string seperator,params object[] args)
{
StringBuilder sb = new StringBuilder();
foreach (var item in args)
{
sb.Append(item + seperator);
}
return sb.ToString();
}
}
using System.Text;
using CSharp6Example.StringExtension;
using System.Console;
namespace CSharp6Example
{
class Program
{
static void Main(string[] args)
{
WriteLine(
JoinAndAddSeperator(" : ",
new object[] { 1, 22, 2, 33, 3, 44 }
));
ReadLine();
}
}
}
public static class LoggerHelper
{
public static string GetMethodName(System.Action action)
{
var name = "missing";
if (action != null && action.Method != null)
{
name = action.Method.Name;
}
return name;
}
}
using System.Text;
using CSharp6Example.LoggerHelper;
using System.Console;
namespace CSharp6Example
{
class Program
{
static void Main(string[] args)
{
GetMethodName(SomeMethod);
}
public static void SomeMethod()
{ }
}
}
public static class LoggerHelper
{
public static string GetMethodName(System.Action action)
{
return action?.Method?.Name;
}
}
public static class LoggerHelper
{
public static string GetMethodName(System.Action action)
{
return action?.Method?.Name ?? "missing";
}
}
public static class Operations
{
public static async Task RunAction(Action action)
{
try {
await SendStartedMessage();
action();
await SendOKMessage();
}
catch
{
await SendError();
}
finally
{
await SendDoneMessage();
}
}
public static async Task SendError() { await Task.FromResult(0); }
public static async Task SendOKMessage() { await Task.FromResult(1); }
public static async Task SendStartedMessage() { await Task.FromResult(-1); }
public static async Task SendDoneMessage() { await Task.FromResult(200); }
}
try
{
action();
}
catch(Exception ex)
{
if (ex.InnerException != null)
{
throw;
}
}
try
{
action();
}
catch(Exception ex) if (ex.InnerException != null)
{
throw;
}
public static void DoIt(string pesel)
{
if (pesel == null)
{
throw new ArgumentNullException("pesel");
}
}
public static void DoIt(string pesel)
{
if (pesel == null)
{
throw new ArgumentNullException(nameof(pesel));
}
}
public class Person
{
private int _id;
public int Id
{
get
{
return _id;
}
}
}
public class Person
{
private int _id = -1;
public int Id
{
get
{
return _id;
}
}
}
public class Person
{
public int Id
{
get; private set;
}
}
public class Person
{
public int Id
{
get; private set;
}
public Person()
{
Id = -1;
}
}
public class Person
{
public int Id { get; } = -1;
}
public class Ruler
{
public Ruler(string unitName, decimal value)
{
UnitsName = unitName;
Value = value;
}
public string UnitsName { get; private set; }
public decimal Value { get; private set; }
}
public class Ruler(string unitsName, decimal value)
{
public string UnitsName { get; } = unitsName;
public decimal Value { get; } = value;
}
public class Ruler(string unitsName, decimal value)
{
public string UnitsName { get; } =
Guard.AgainstNull<string>("unitName",unitsName);
public decimal Value { get; } = value;
}
public class Ruler(string unitsName, decimal value)
{
public string UnitsName { get; } = unitsName ?? "cm";
public decimal Value { get; } = value;
}
public static decimal ParseEase(string number)
{
var amount = 0m;
var boolean = decimal.TryParse(number,out amount);
return amount;
}
public static decimal ParseEase(string number)
{
if (decimal.TryParse(number, out var amount))
return amount;
return 0m;
}
public static double GetNumbersDividedBy3AndDoWeirdMath()
{
int result = 0;
var list = new List<int>() { 1212, 6454, 121584, 3435, 67, 43652, 343 };
//var collection = list.Where(n => n % 3 == 1).ToList();
foreach (var item in var collection = list.Where(n => n % 3 == 1).ToList())
{
result = result + (item / collection.Count) ;
}
return result;
}