public class Operations
{
public string Name() => Environment.UserName;
}
public class FileOperations
{
public string Path => AppDomain.CurrentDomain.BaseDirectory;
}
public class Helpers
{
public static T GetObject<T>() where T : new() => new T();
}
public class Demos
{
public static void Run()
{
Operations result1 = Helpers.GetObject<Operations>();
Console.WriteLine(result1.Name());
FileOperations result2 = Helpers.GetObject<FileOperations>();
Console.WriteLine(result2.Path);
}
}
Demos.Run();
using System;
using System.Collections.Generic;
namespace BetweenCodeSample
{
/// <summary>
/// Example for a generic language extension for <see cref="IComparable"/>
/// In this case the extension is used for int and DateTime
/// </summary>
class Program
{
static void Main(string[] args)
{
int age = 29;
Console.WriteLine($"{age,-3} is over 30 {age.Between(30, 30).ToYesNo()}");
age = 30;
Console.WriteLine($"{age,-3} is over 30 {age.Between(30, 30).ToYesNo()}");
age = 30;
Console.WriteLine($"{age,-3} is between 19 and 30 {age.Between(19, 30).ToYesNo()}");
age = 12;
Console.WriteLine($"is child {age.IsChild().ToYesNo()}");
DateTime lowDateTime = new (2022, 1, 1);
DateTime someDateTime = new (2022, 1, 2);
DateTime highDateTime = new (2022, 1, 8);
Console.WriteLine($"{someDateTime:d} between {lowDateTime:d} and {highDateTime:d}? {someDateTime.Between(lowDateTime, highDateTime).ToYesNo()}");
someDateTime = new DateTime(2022, 2, 2);
Console.WriteLine($"{someDateTime:d} between {lowDateTime:d} and {highDateTime:d}? {someDateTime.Between(lowDateTime, highDateTime).ToYesNo()}");
Console.Read();
}
}
/// <summary>
/// Place this class in it's own file
/// </summary>
public static class GenericExtensions
{
public static bool Between<T>(this T value, T lowerValue, T upperValue)
where T : struct, IComparable<T>
=> Comparer<T>.Default.Compare(value, lowerValue) >= 0 &&
Comparer<T>.Default.Compare(value, upperValue) <= 0;
public static bool IsChild(this int sender)
=> sender.Between(1, 12);
public static bool IsOver30(this int sender)
=> sender.Between(30, 30);
public static string ToYesNo(this bool value)
=> value ? "Yes" : "No";
}
}
30.IsOver30()
doesn't seem to be right.Yep, your right.Hmmm..30.IsOver30()
doesn't seem to be right.
public static bool IsOver30(this int sender) => sender.IsGreaterThan(30);
public static bool IsGreaterThan<T>(this T sender, T other) where T : IComparable
=> sender.CompareTo(other) > 0;
public static bool IsLessThan<T>(this T sender, T other) where T : IComparable
=> sender.CompareTo(other) < 0;
It's not right, it'sHmmm..30.IsOver30()
doesn't seem to be right.
false
That is a female perspectiveIt's not right, it'sfalse
![]()
No, it's a Boolean expression.That is a female perspective![]()