Skip to content

Instantly share code, notes, and snippets.

View Blind-Striker's full-sized avatar

Deniz İrgin Blind-Striker

View GitHub Profile
@Blind-Striker
Blind-Striker / PatternMatchingWithOut.cs
Created September 6, 2017 16:08
C# 7.0 Pattern Matching and Out Example for blog
public static void ShowUserBalance(object balance)
{
if (balance is decimal userBalance || (balance is string s && decimal.TryParse(s, out userBalance)))
{
Console.WriteLine($"Kullanıcının bakiyesi {userBalance}");
}
}
@Blind-Striker
Blind-Striker / SwitchCase.cs
Created September 6, 2017 16:11
C# 7.0 Swith-Case Example for blog
switch(shape)
{
case Circle c:
WriteLine($"circle with radius {c.Radius}");
break;
case Rectangle s when (s.Length == s.Height):
WriteLine($"{s.Length} x {s.Height} square");
break;
case Rectangle r:
WriteLine($"{r.Length} x {r.Height} rectangle");
@Blind-Striker
Blind-Striker / LocalFunctions.cs
Created September 6, 2017 16:17
C# 7.0 Local Functions example for blog
public int Fibonacci(int x)
{
if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x));
{
return Fib(x).current;
}
(int current, int previous) Fib(int i)
{
if (i == 0) return (1, 0);
@Blind-Striker
Blind-Striker / RefReturn.cs
Created September 6, 2017 16:55
C# 7 Ref Returns for Blog
public ref int Find(int number, int[] numbers)
{
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] == number)
{
return ref numbers[i]; // burada dönülen aslında i. elemanın değeri değil onun array içerisindeki referansı
}
}
@Blind-Striker
Blind-Striker / ExpressionBodiedOld.cs
Created September 6, 2017 16:58
C# 7.0 Expression Bodied example for blog
public class Square
{
public Square(int length)
{
Length = length;
}
~Square() // Örnek amaçlı yazıldı
{
Length = 0;
@Blind-Striker
Blind-Striker / ExpressionBodiedNew.cs
Created September 6, 2017 16:59
C# 7.0 Expression Bodied new example for blog
public class Square
{
public Square(int length) => Length = length;
~Square() => Length = 0; // Örnek amaçlı yazıldı
public int Length { get; set; }
public int Area => Length * Length;
public int Perimeter => 4 * Length;
public override string ToString() => $"Square: Area={Area}, Perimeter={Perimeter}";
}
@Blind-Striker
Blind-Striker / ThrowExpressions.cs
Created September 6, 2017 17:01
C# Throw expressions example for blog
class Person
{
public string Name { get; }
public Person(string name) => Name = name ? ? throw new ArgumentNullException(name);
public string GetFirstName()
{
var parts = Name.Split(" ");
return (parts.Length > 0) ? parts[0] : throw new InvalidOperationException("No name!");
}
@Blind-Striker
Blind-Striker / BankOpsWithoutAop.cs
Last active September 7, 2017 13:47
Blog example for without AOP
public class BankOperations : IBankOperations
{
private readonly ILogger _logger;
private readonly IExceptionHandling _exceptionHandling;
private readonly ISecurityService _securityService;
private readonly IAccountOperations _accountOperations;
public BankOperations(ILogger logger, IExceptionHandling exceptionHandling, ISecurityService securityService, IAccountOperations accountOperations)
{
_logger = logger;
@Blind-Striker
Blind-Striker / BankOpsWithAop.cs
Created September 7, 2017 14:13
Blog example for AOP
public class BankOperations : IBankOperations
{
private readonly IAccountOperations _accountOperations;
public BankOperations(IAccountOperations accountOperations)
{
_accountOperations = accountOperations;
}
public void Transfer(string fromAccountNumber, string toAccountNumber, decimal ammount)
@Blind-Striker
Blind-Striker / Postsharp.cs
Last active September 7, 2017 14:24
Postsharp example for AOP blog Article
public class LoggingAspect : OnMethodBoundaryAspect
{
public override void OnEntry(MethodExecutionArgs args)
{
ILogger logger = new Logger();
logger.LogOperation(args.Method.Name, args[0], args[1], args[2]);
}
// Diğer methodlar, bilgilendirme amaçlı