Skip to content

Instantly share code, notes, and snippets.

View mattumotu's full-sized avatar

Matt Phillips mattumotu

  • United Kingdom
View GitHub Profile
public interface IFetchThings<T> {
T Fetch();
}
public interface ISecureFetchThings<T> : IFetchThings<T> {
T Fetch();
bool isLoggedOn();
void logOn();
}
public interface IFetchThings<T> where T : Fetched, IFetchable, System.IComparable<T>, new() {
...
}
@mattumotu
mattumotu / gist:e3347b7095309da7cc2bd41d98bc98f0
Created December 3, 2018 14:06
generics-where-class.cs
public interface IFetchThings<T> where T : class {
T Fetch();
}
public class Dictionary<TKey,TValue> {
public void Add (TKey key, TValue value);
public bool ContainsKey (TKey key);
public bool ContainsValue (TValue value);
}
public class simpleExample<T> {
private T myVar;
public T myProp { get; set; }
public T myMethod1() { ... }
public void myMethod2(T arg) { ... }
public T myMethod3(T arg) { ... }
}
public class DocumentFetcher : IFetchThings<Document> {
public Document Fetch() { ... }
}
public class ProjectFetcher : IFetchThings<Project> {
public Project Fetch() { ... }
}
public class ClientFetcher : IFetchThings<Client> {
public Client Fetch() { ... }
}
public interface IFetchThings<T> {
T Fetch();
}
IFetchThings fetcher = new ClientFetcher();
object fetched = fetcher.Fetch(); // object is really a Client (Boxed as an object)
// let's convert the object to a client
Client c = (Client)fetched; // Unbox fetched into Client - performance hit
// the compiler can't see any problem with this
// and it works at run-time
// let's convert the object to a project
Project p = (project)fetched; // Unbox fetched into Project - performance hit