Skip to content

Instantly share code, notes, and snippets.

@Plus1XP
Plus1XP / RelayCommand.cs
Created October 14, 2019 16:08
RelayCommand.cs
public class RelayCommandAsync<T> : ICommand
{
private readonly Func<T, Task> executedMethod;
private readonly Func<T, bool> canExecuteMethod;
public event EventHandler CanExecuteChanged;
public RelayCommandAsync(Func<T, Task> execute) : this(execute, null) { }
public RelayCommandAsync(Func<T, Task> execute, Func<T, bool> canExecute)
{
@Plus1XP
Plus1XP / CharacterReplacement.cs
Created April 13, 2019 18:06
Iterates through a string replacing characters with a character of your choice
public string ReplaceLetters(string wordToSearch, char letterToFind, char letterToReplace)
{
StringBuilder word = new StringBuilder(wordToSearch);
int index = 0;
foreach (char letter in wordToSearch)
{
if (letter.Equals(letterToFind))
{
@Plus1XP
Plus1XP / CalculateAverage.cs
Created April 13, 2019 17:28
Calculate the average value using a foreach loop vs. a LINQ function.
public void CalculateAverage()
{
// Create and populate the list of numbers
List<double> values = new List<double>();
values.Add(1);
values.Add(5);
values.Add(21);
// Calculating the average, using a foreach
@Plus1XP
Plus1XP / RandomNumberGenerator.cs
Created April 13, 2019 17:16
This method uses an instance of an encryption class (RNGCryptoServiceProvider). This class is better at not following a pattern when it creates random numbers.
public static class RandomNumberGenerator
{
private static readonly RNGCryptoServiceProvider _generator = new RNGCryptoServiceProvider();
public static int NumberBetween(int minimumValue, int maximumValue)
{
byte[] randomNumber = new byte[1];
_generator.GetBytes(randomNumber);
@Plus1XP
Plus1XP / ObjectCopier.cs
Last active April 16, 2019 09:43
Copies Fields & Properties from one class to another
static class CopyObject
{
public static void ShallowCopy(this Object dst, object src)
{
var srcT = src.GetType();
var dstT = dst.GetType();
foreach (var f in srcT.GetFields())
{
var dstF = dstT.GetField(f.Name);
if (dstF == null)
@Plus1XP
Plus1XP / OpenSaveDialogueBox.cs
Created August 1, 2018 16:13
WinForms Open/Save Dialogue Box
private void buttonLoadFile_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open File";
ofd.Filter = "All Files (*.*)|*.*";
textBoxName.Text = ofd.SafeFileName;
textBoxLocation.Text = ofd.FileName;
if (ofd.ShowDialog() == DialogResult.OK)
@Plus1XP
Plus1XP / DoFizzBuzz.cs
Created March 13, 2018 02:24
Fizz Buzz using Bools
public void DoFizzBuzz()
{
for (int i = 1; i <= 100; i++)
{
bool fizz = i % 3 == 0;
bool buzz = i % 5 == 0;
if (fizz && buzz)
Console.WriteLine ("FizzBuzz");
else if (fizz)
Console.WriteLine ("Fizz");
@Plus1XP
Plus1XP / CalculatePower.cs
Last active March 12, 2018 11:27
Method to calculate the power of an int without using the Math.Pow Method.
private long CalculatePower(int Number, int PowerOf)
{
long Result = Number;
for (int i = PowerOf; i > 1; i--)
{
Result = (Result * Number);
}
return Result;
}
@Plus1XP
Plus1XP / PropertyLogic.cs
Last active March 12, 2018 11:31
Set logic for a Property
private int this.age;
public int Age
{
get
{
return this.age;
}
set
{