Skip to content

Instantly share code, notes, and snippets.

View enisn's full-sized avatar

Enis Necipoglu enisn

View GitHub Profile
@enisn
enisn / IDataProvider.cs
Created May 9, 2020 20:24
Contravariance & Covariance - Sample 1
public interface IDataProvider<TData>
{
TData GetData();
}
@enisn
enisn / Program.cs
Created May 8, 2020 21:30
NullChecks - Sample 7
public void DoSomething(NotNull<string> message, NotNull<int> id, NotNull<Product> product)
{
// ...
}
@enisn
enisn / Program.cs
Last active May 9, 2020 09:09
NullChecks - Sample 5
static void Main(string[] args)
{
DoSomething("Hello World!"); // Works perfectly 👌
DoSomething(null); // Throws ArgumentNullException at runtime.
string parameter = null;
DoSomething(parameter); // Throws ArgumentNullException at runtime.
}
@enisn
enisn / NotNull{T}.cs
Created May 8, 2020 21:19
NullChecks - Sample 3
public class NotNull<T>
{
public NotNull(T value)
{
this.Value = value;
}
public T Value { get; set; }
public static implicit operator NotNull<T>(T value)
@enisn
enisn / NullChecks.cs
Created May 8, 2020 21:12
Nullcheck - Sample 2
public void DoSomething([NotNull]string message) // Does not affect anything at runtime.
{
}
public void AnotherMethod()
{
DoSomething(null); // MsBuild doesn't allow to build.
string parameter = null;
DoSomething(parameter); // MsBuild allows build. But nothing happend at runtime.
}
@enisn
enisn / NullChecks.cs
Created May 8, 2020 21:06
NullCheck - Sample 1
public void DoSomething(string message)
{
if(message == null)
throw new ArgumentNullException();
// ...
}
@enisn
enisn / BindingExtensions.cs
Created May 7, 2020 21:13
Challenge #2 - Solution - Entire
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace SomeCompany.Extensions
{
public static class BindingExtensions
{
public static string BindObjectProperties(this string str, object obj)
@enisn
enisn / StringBinding.cs
Last active August 2, 2022 07:45
Challenge #2 - Solution - 7
// This will work perfectly for nested objects.
var template = "Welcome back {Name}. You're logged in from {Profile.Address.Country.Code}. Have a good day.";
var user = new User { Name = "John", Address = new Address { Country = new Country { Code = "US" } } };
var message = template.BindObjectProperties(user);
Console.WriteLine(message);
@enisn
enisn / BindingExtensions.cs
Created May 7, 2020 21:09
Challenge #2 - Solution - BindObjectProperties Last
public static string BindObjectProperties(this string str, object obj)
{
if (obj == null) return str;
foreach (var item in ExtractParams(str))
{
str = str.Replace("{" + item + "}", obj.GetPropValue(item)?.ToString());
}
return str;
}
@enisn
enisn / StringBinding.cs
Created May 7, 2020 21:07
Challenge #2 - Solution - 6.1
// This will not work with nested object members.
var template = "Welcome back {Name}. You're logged in from {Profile.Address.Country.Code}. Have a good day.";
var user = new User { Name = "John", Email = "[email protected]" };
var message = template.BindObjectProperties(user);
Console.WriteLine(message);