Example class
public class TrickCollection
{
// https://stackoverflow.com/questions/9033/hidden-features-of-c?page=2&tab=votes#tab-top
public string Chain(string arg1, string arg2, string arg3) =>
arg1 ?? arg2 ?? arg3 ?? String.Empty;
public bool OptimizedStringComparison(string str1, string str2) =>
String.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase);
// avoid combining path strings yourself. Use the method
public void CombinePath()
{
string assemblyPath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string filename = "crap.txt";
string path = System.IO.Path.Combine(assemblyPath, filename);
Console.WriteLine(path);
}
// If you want to exit your program without calling
// any finally blocks or finalizers use FailFast
public void FailFast() =>
Environment.FailFast("Bye...");
// Use C# keywords as variable names (string int...)
public void KeywordVariables()
{
int @int = 0;
string @string = "Crap";
Console.WriteLine(@int + " " + @string);
}
// [as, is] operators usage
public void Aziz_Light()
{
// https://www.youtube.com/watch?v=IKmRtJcRX_I
// IS some object equal to certain type?
object obj = "Crap";
if (obj is string) Console.WriteLine("Same type");
// can we convert that object AS some other object?
int? x = obj as int?; // x will be null if conversion fails
}
public SomeEnum GetEnumByNumber(int n) =>
(SomeEnum)n;
public bool CompareEnumAndNumber(int n, SomeEnum someEnum) =>
n == (int)someEnum;
// method that instantiates object from class
// that implements another class or interface
// (probably overkill)
public T Construct<T, U>() where U : T, new()
{
return new U();
}
public DateTime FormatDateFromString(string dateString)
{
var date = DateTime.ParseExact(dateString, "d/M/yyyy", CultureInfo.InvariantCulture);
return date;
}
public string FormatStringFromDate(DateTime date)
{
string dateString = String.Format($"{date:yyyyMdd}"); // any format is possible
return dateString;
}
public string ConvertToBinary(int value, int numOfBits) =>
(numOfBits > 1 ? ConvertToBinary(value >> 1, numOfBits - 1) : null) + "01"[value & 1];
public int[] NullFilter(int?[] nullers) =>
return nullers.OfType().ToArray();
// most efficient way to calculate fibonacci series from 0 to n
public void Fibonacci(int n)
{
double sq = Math.Sqrt(5);
for (int i = 0; i < n; i++)
{
double number = 1 / sq * (Math.Pow((1 + sq) / 2, i) - Math.Pow((1 - sq) / 2, i));
Console.WriteLine(number) ;
}
}
// cipher (and decipher) a message
public void Cipher(string message, string password)
{
int plen = password.Length;
var pChars = new byte[plen];
for (int i = 0; i < plen; i++)
{
pChars[i] = (byte)password[i];
}
int mlen = message.Length;
var mChars = new byte[mlen];
for (int i = 0; i < mlen; i++)
{
mChars[i] = (byte)message[i];
}
int counter = 0;
var encoded = new int[mlen];
for (int i = 0; i < mlen; i++)
{
if (counter == plen)
counter = 0;
encoded[i] = mChars[i] ^ pChars[counter];
}
string encodedMessage = "";
for (int i = 0; i < mlen; i++)
{
encodedMessage += (char)encoded[i];
}
var cryptoEventArgs = new CryptoEventArgs()
{
EncodedMessage = encodedMessage,
Notification = "Message encrypted"
};
}
}