Skip to content

Instantly share code, notes, and snippets.

Avatar

Stefan Mehnert zs40x

View GitHub Profile
View SingleResponsibilityPrinciple_Valid.cs
public class Sensor
{
public string Name { get; }
public List<double> MeasuredValues { get; }
public double AverageValue
=> MeasuredValues.Average();
public double MinimumValue
=> MeasuredValues.Min();
public double MaximumValue
=> MeasuredValues.Max();
View SingleResponsibilityPrinciple_Violation.cs
public class Sensor
{
public string Name { get; }
public List<double> MeasuredValues { get; }
public double AverageValue
=> MeasuredValues.Average();
public double MinimumValue
=> MeasuredValues.Min();
public double MaximumValue
=> MeasuredValues.Max();
View AvgTemperatureReader_OCP_Valid.cs
namespace Open_Closed_Principle.Valid
{
public class AvgTemperatureReader
{
public double GetAverageTemperature(List<ITemperatureSensor> sensors)
{
return sensors.Count == 0 ? 0 : sensors.Average(s => s.CurrentTemperature.Value);
}
}
}
View AvgTemperatureReader_OCP_Violation.cs
namespace Open_Closed_Principle.Violation
{
public class AvgTemperatureReader
{
public double GetAverageTemperature(List<object> temperatureSensors)
{
double total = 0;
int count = 0;
foreach (var temperatureSensor in temperatureSensors.OfType<IndoorTemperatureSensor>())
View LSP_ViolationTests.cs
namespace LiskovSubstitutionPrinciple.Tests
{
[TestFixture]
public class Violation
{
[Test]
public void RectangleIsOkay()
{
Rectangle rect = new Rectangle
{
View LSP_RectangleAndSquare.cs
namespace LiskovSubstitutionPrinciple.Violation
{
public class Rectangle
{
protected int _width;
protected int _height;
public virtual int Height
{
get { return _height; }
View yield_return_example_2.cs
class Person
{
public int Age { get; }
public string Name { get; }
public Person(int age, string name)
{
this.Age = age;
this.Name = name;
}
View yield_return_example_1.cs
public class Program
{
public static void Main(string[] args)
{
PowerOf2Example();
}
private static void PowerOf2Example()
{
var numbers = new List<int>() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
View null_conditional_operator.cs
class Engine
{
public bool IsRunning { get; private set; }
public void Start() { }
public void Stop() { }
}
class Machine
{
public Engine Engine { get; }
View null_coaesceing_operator.cs
using System;
namespace ConsoleApplication
{
public class Program
{
static void Main()
{
Console.WriteLine(getWithTernaryOperator());
Console.WriteLine(getWithNullCoalescingOperator());