Skip to content

Instantly share code, notes, and snippets.

View PhiHuyHoang's full-sized avatar
🎯
Focusing

Hoang Phi PhiHuyHoang

🎯
Focusing
  • SAP
  • Budapest, Hungary
View GitHub Profile
@PhiHuyHoang
PhiHuyHoang / example-2.cs
Last active September 12, 2018 21:04
method constructor function example C#
private delegate string MyDelegate(string s1, string s2);
@PhiHuyHoang
PhiHuyHoang / example-1.cs
Last active September 12, 2018 21:05
method constructor function example C#
public string WhatUp(string firstName, string lastName)
{
return $"What's up {firstName} {lastName}";
}
@PhiHuyHoang
PhiHuyHoang / example-3.cs
Created September 12, 2018 21:10
method constructor function example C#
Action<string> logMethods;
public void AddLogMethod(Action<string> newLogMethod)
{
logMethods += newLogMethod;
}
public void LogThis(string msg)
{
logMethods?.Invoke($"[{DateTime.Now.ToLongDateString()}] {msg}");
@PhiHuyHoang
PhiHuyHoang / example-4.cs
Created September 12, 2018 21:22
method constructor function example C#
static void ConsoleLog(string s)
{
Console.WriteLine($"[CONSOLE] {s}");
}
static void Main(string[] args)
{
/// section 1 ///
Logger log = new Logger();
log.AddLogMethod(ConsoleLog);
log.AddLogMethod(delegate (string s) { Console.WriteLine($"[ANON] {s}"); });
@PhiHuyHoang
PhiHuyHoang / example-5.cs
Created September 12, 2018 21:47
method constructor function example C#
log.LogThis("Hello, delegates"); Thread.Sleep(1000);
log.LogThis("Apache started..."); Thread.Sleep(1000);
log.LogThis("Mysql started..."); Thread.Sleep(1000);
log.LogThis("Exim started..."); Thread.Sleep(1000);
log.LogThis("Pureftpd started..."); Thread.Sleep(1000);
log.LogThis("Influxdb started..."); Thread.Sleep(1000);
log.LogThis("Google started..."); Thread.Sleep(1000);
log.LogThis("Git started..."); Thread.Sleep(1000);
log.LogThis("Docker started..."); Thread.Sleep(1000);
log.LogThis("Apache stopped..."); Thread.Sleep(1000);
@PhiHuyHoang
PhiHuyHoang / example-6.cs
Last active September 12, 2018 21:50
method constructor function example C#
List<string> filtered = log.Filter(entry => entry.ToLower().Contains("apache")); // Actually this is LinQ, very cool. I'll talk about this later.
foreach(string s in filtered)
{
Console.WriteLine(s);
}
@PhiHuyHoang
PhiHuyHoang / example-7.cs
Created September 12, 2018 21:56
method constructor function example C#
List<string> entries;
public Logger()
{
entries = new List<string>();
AddLogMethod(msg => entries.Add(msg)); //or we can use: AddLogMethod(entries.Add);
}
//Example for Func
@PhiHuyHoang
PhiHuyHoang / syntax-extension-method.cs
Created September 19, 2018 21:26
Syntax for Extension Methods
public static class [YOUR_CLASS_NAME]
{
public static [RETURN_TYPE] [METHOD_NAME](this [TYPE_NAME] s)
{
// TODO: Write code here
}
}
/*
---[YOUR_CLASS_NAME]---: What ever you like
---[RETURN_TYPE]---: What ever you want
public static class HoangExtension
{
public static void PlusWithHoang(this string s)
{
Console.WriteLine($"{s} - Make from HoangExtension");
}
}
class Program
{
class Post
{
public string status { get; set; }
public DateTime timestamp { get; set; }
}