Skip to content

Instantly share code, notes, and snippets.

View georgebearden's full-sized avatar

George Bearden georgebearden

View GitHub Profile
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
public class Program
{
public static string ReverseString(string input)
{
if (input == null)
using System;
namespace FuncOverloads
{
public class Program
{
public static void Main( string[] args )
{
Func<int> funcReturnsInt = () => { return 0; };
@georgebearden
georgebearden / SocketWatcher.cs
Created September 16, 2015 17:47
An example of a singleton rx observable that will notify when a socket is connected/disconnected
public enum SocketStates
{
Unknown,
Up,
Down
}
public class SocketWatcher : IObservable<SocketStates>, IDisposable
{
private readonly BehaviorSubject<SocketStates> _socketState = new BehaviorSubject<SocketStates>(SocketStates.Unknown);
public static int Min(this IEnumerable<int> ints)
{
  return ints.Aggregate((min, next) => min <= next ? min : next);
}
@georgebearden
georgebearden / SingletonObservable.cs
Last active September 10, 2015 20:06
How to create an IObservable that returns the same instance of the observable for each subscription
public class SingletonObservable : IObservable<DateTime>, IDisposable
{
private readonly Subject<DateTime> _subject = new Subject<DateTime>();
private bool _isRunning = true;
public SingletonObservable()
{
InstanceCount++;
Task.Run( async () =>

I've always heard that one should not call overridable methods in constructors, and doing so will cause a violation during compilation. To understand why a build warning is generated, lets look at an example.

First, let me state something that we all should hopefully find reasonable. When instantiating an instance of a class, the first thing that should be called is the class' constructor. So assume we have a class that logs strings to a file:

public class FileLogger
{ 
  private readonly string _logDirectory;
  
  public FileLogger(string logDirectory)

Coming from a background that was not heavy on callbacks, whenever I wanted to short-circuit a method I would simply "return".
For example:

public void Log(string data) 
{
  if (data == null)
    return;
    
  ...
}
@georgebearden
georgebearden / ReactiveServer.cs
Created July 24, 2015 15:10
A sample implementation of using Rx.NET on top of a C# HttpListener
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Tests
{