Skip to content

Instantly share code, notes, and snippets.

View asarnaout's full-sized avatar

Ahmed Shirin asarnaout

  • Tampa, FL
View GitHub Profile
@asarnaout
asarnaout / ProgrammingToAnAbstraction.cs
Last active December 3, 2017 19:10
Programming to an Abstraction
/*
* Several times developers ask questions like:
*
* -What are IEnumerables, ICollections and ILists? Why should we use them?
* -What's the big deal about all those interfaces? I've always been using Concrete types and they get the job done!
* -Why are we using 'var's everywhere?
*
* Abstract programming allows developers to decouple their code from concrete implementations that could be regularly changed. Writing
* abstract code that adapts to modifications is a main characteristic of high quality code. Future proofing your code by using abstract
* types instead of concrete ones will allow API developers to roll out changes to their code without making their clients' lives a bit
@asarnaout
asarnaout / A.Brief.Review.Of.Covariance-Contravariance.cs
Last active December 9, 2017 20:46
Covariance & Contravariance in .NET
/*
* In C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments.
* Covariance preserves assignment compatibility and contravariance reverses it.
*/
/************************************************************* Covariance *************************************************************/
/*
* Covariance for enables implicit conversion of a more derived generic type to a less derived generic type.
*
@asarnaout
asarnaout / A.OOP_Basics.cs
Last active January 25, 2018 17:11
Object Oriented Basics in C#
/*
* In this gist we will discuss OOP basics, this topic will cover:
*
* -Classes, properties, attributes, and methods.
* -Access modifiers.
* -Static vs Non-static methods.
* -Exceptions
* -Casting
*/
@asarnaout
asarnaout / A.PrivateMethodInvoker.cs
Last active November 29, 2018 07:05
Invoking Private Methods During Runtime
/// <summary>
/// A simple library to invoke private methods during runtime. Could be used while unit testing private functions.
/// </summary>
public class MethodInvoker
{
private static MethodInfo GetMethod<T>(string methodName) =>
typeof(T).GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);
/// <summary>
/// Invokes a private void method.
@asarnaout
asarnaout / A.Database-Transactions.cs
Last active March 9, 2018 11:47
Database Transactions & EntityFramework
public class Program
{
private static TestingContext Context { get; set; }
public static void Main(string[] args)
{
/*
* A database transaction is a unit of work that represents a change in the database and is treated in a
* coherent and reliable way independent of other transactions.
*
@asarnaout
asarnaout / Async-Await-Tutorial.cs
Created March 11, 2018 14:07
Async/Await Tutorial in C#
public class Program
{
public static void Main(string[] args)
{
DoSomethingAsync();
Console.ReadLine();
}
public static async void DoSomethingAsync()
{
@asarnaout
asarnaout / A.DelegatesTutorial.cs
Created March 11, 2018 14:35
Delegates and Events Tutorial in C#
using System;
namespace Delegates.Tutorial
{
public class DelegatesTutorial
{
public delegate void TestDelegate(int x, bool y);
// A delegate declaration gets translated to a class of the same name by the compiler, this class inherits from the
// MulticastDelegate base class. The MulticastDelegate holds an InvocationList for each instance of the delegate, which is a
// list of all the methods called via the delegate in the same order as they are called
@asarnaout
asarnaout / Semaphores.cs
Created March 11, 2018 15:34
Async/Await & Concurrency Control in C#
public class Program
{
private static object Locker { get; set; }
private static async void AwaitWithLock()
{
/*
* Whenever you have code that might be contending over some resource, then you can place it in a lock statement
* to ensure thread safety. However, lock statements do not support async operations; you cannot await some task
* inside a locked code block. This was actually implemented to prevent deadlocks as the async operation might
@asarnaout
asarnaout / A.Visitor.Design.Pattern.cs
Last active May 4, 2018 04:26
Visitor Design Pattern
namespace Visitor
{
class Program
{
/*
* The Visitor pattern allows you to define new operations on a type without changing the type itself.
*/
static void Main(string[] args)
{
var employee = new Employee
@asarnaout
asarnaout / A.Mediator.Design.Pattern.cs
Last active May 4, 2018 04:27
Mediator Design Pattern
namespace Mediator
{
class Program
{
/*
* With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate
* directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects,
* thereby reducing coupling.
*/
static void Main(string[] args)