Skip to content

Instantly share code, notes, and snippets.

@NineMvp
NineMvp / PrivateObject<T>.cs
Last active May 4, 2018 06:04
Simple PrivateObject
// Test Class
public class Demo {
private string send (String msg) => $"{msg} : Sent.";
}
// Test Util
public class PrivateObject<T> where T : class, new () {
private T obj;
public PrivateObject () {
obj = new T ();
@NineMvp
NineMvp / demo1.cs
Created November 3, 2017 19:44
nullable primitive data type
int? total = 100;
total ?? 0; //เข้าถึงข้อมูลแบบกำหนดค่า default value หากเป็น null
@NineMvp
NineMvp / returnRefAndLocal.cs
Last active October 21, 2016 08:11
Return Ref Local
static void Main()
{
var pers = new int[] { 100, 101, 102, 109 };
ref var index2 = ref getValue(pers, 2);
Console.WriteLine($"index 2 before = { index2 } "); // 102
index2 += 5;
Console.WriteLine($"index 2 after = { index2 } "); //107
Console.WriteLine($"index 2 after = { pers[2] } "); //107
@NineMvp
NineMvp / localfunc.cs
Created October 21, 2016 07:30
Local Function
void Register(UserProfile userProfile) {
//local function
Tuple<string, string, int> createMember(string fName, string lName, int Age) => Tuple.Create<string, string, int>(fName, lName, Age);
//call local function
var newMember = createMember(userProfile.fname, userProfile.lname, userProfile.age);
}
@NineMvp
NineMvp / NewTuple.cs
Last active December 24, 2016 17:57
New Tuple
//Old way to create new Tuple object
var tp1 = Tuple.Create<string, string, int>("Noppon", "DevRock", 26);
Console.WriteLine($" tp1 FName={tp1.Item1}, LName={tp1.Item2}, Age={tp1.Item3}");
//New in C#7.0
var newTuple = ("Noppon", "DevRock", 26);
Console.WriteLine($" newTuple FName={newTuple.Item1}, LName={newTuple.Item2}, Age={newTuple.Item3}");
//Tuple Literals
var newTupleLiterals = (fname: "Noppon", lname: "DevRock", age: 26);
@NineMvp
NineMvp / literals.cs
Created October 21, 2016 04:28
Numer Literals
var dec = 1_000_000_000_000;
var hex = 0xAB_CD_EF;
var bin = 0b1010_1011_1100_1101_1110_1111;
@NineMvp
NineMvp / pattern-maching.cs
Last active October 19, 2016 15:17
Pattern Matching
static bool PatternMatching(object x) {
// 'is' pattern variable
if (x is int i && i > 0) return true;
if (x is string k && k.Trim().Length > 0) return true;
if (x is person p && p != null) return true;
else return false;
// switch statement with pattern 'case' and 'when'
switch (x) {
case int a when a > 0: return true;
@NineMvp
NineMvp / outVars.cs
Last active October 19, 2016 04:20
c#7.0 out variables feature
void Run()
{
OutVariables(out int x, out int y);
Console.Write($"{x}, {y}");
}
void OutVariables(out int x, out int y) => x = (y = 2) * 2;
@NineMvp
NineMvp / java_switch_version.sh
Last active May 31, 2016 08:36
switch java version base script
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
#default java8
export JAVA_HOME=$JAVA_8_HOME
#credit http://stackoverflow.com/users/1037852/vegard
@NineMvp
NineMvp / QueryString.js
Last active May 12, 2016 03:13
QueryString Helper
var QueryString = function () {
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
} else if (typeof query_string[pair[0]] === "string") {
var arr = [query_string[pair[0]], decodeURIComponent(pair[1])];