Description

The snippets on this wikipage are written for the .NET Framework v4.0 and higher, but most probably work in older versions. Most will, however not work for .NET Core and .NET Standard.

Package Specific snippits

Package

Description

Colore

C# Library for Razer Chroma's SDK

Snippets

override GetHashCode()

   1 public override int GetHashCode()
   2         {
   3             int hash = 17;
   4             hash = hash * 23 + intA;
   5             hash = hash * 23 + intB;
   6             hash = hash * 23 + objectC.GetHashCode();
   7             return hash;
   8         }

Number Remap Class

   1 public static class Remap
   2     {
   3         private static double Map(double value, double fromSource, double toSource, double fromTarget, double toTarget)
   4         {
   5             return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
   6         }
   7     }

Custom Events & Handling

CustomEventArgs.cs

   1 public class CustomEventArgs : EventArgs
   2 {
   3     private readonly object item;
   4 
   5     public object Item
   6     {
   7         get { return item; }
   8     }
   9 
  10     public CustomEventArgs(object item)
  11     {
  12         this.item = item;
  13     }
  14 }

Instance.cs

   1 public delegate void NotifyCustomEvent(object sender, CustomEventArgs e);
   2 public event NotifyCustomEvent CustomEvent;
   3 
   4 private void RaiseCustomEvent(obj sender,  obj item)
   5 {
   6     CustomEvent?.Invoke(sender, new CustomEventArgs(item));
   7 }

InstanceOwner.cs

   1 public InsanceOwner()
   2 {
   3   Instance instance = new instance();
   4   instance.CustomEvent += new Instance.NotifyCustomEvent(CustomEventHandler);
   5 }
   6 
   7 public void CustomEventHandler(obj sender, CustomEventArgs e)
   8 {
   9     //Handle that custom shit
  10 }

IComparable Class

   1 public class Mission : IComparable<Mission>, IComparable
   2 {
   3   public string Name { get; private set; }
   4   public DateTime LaunchDate { get; private set; }
   5   public DateTime ReturnDate { get; private set; }
   6   public string SpaceShip { get; private set; }
   7 
   8   public List<Astronaut> Astronauts { get; private set; }
   9 
  10   public Mission(string name, DateTime launchDate, DateTime returnDate, string spaceShip)
  11   {
  12       Astronauts = new List<Astronaut>();
  13       Name = name;
  14       LaunchDate = launchDate;
  15       ReturnDate = returnDate;
  16       SpaceShip = spaceShip;
  17   }
  18 
  19   public int CompareTo(Mission other)
  20   {
  21       if (ReferenceEquals(this, other)) return 0;
  22       if (ReferenceEquals(null, other)) return 1;
  23       int nameComparison = string.Compare(Name, other.Name, StringComparison.Ordinal);
  24       if (nameComparison != 0) return nameComparison;
  25       int launchDateComparison = LaunchDate.CompareTo(other.LaunchDate);
  26       if (launchDateComparison != 0) return launchDateComparison;
  27       int returnDateComparison = ReturnDate.CompareTo(other.ReturnDate);
  28       if (returnDateComparison != 0) return returnDateComparison;
  29       return string.Compare(SpaceShip, other.SpaceShip, StringComparison.Ordinal);
  30   }
  31 
  32   public int CompareTo(object obj)
  33   {
  34       if (ReferenceEquals(null, obj)) return 1;
  35       if (ReferenceEquals(this, obj)) return 0;
  36       if (!(obj is Mission)) throw new ArgumentException($"Object must be of type {nameof(Mission)}");
  37       return CompareTo((Mission) obj);
  38   }
  39 }

Howto/CSharp (last edited 2020-04-20 11:02:18 by Burathar)