Skip to content

Instantly share code, notes, and snippets.

@RobinHAEVG
RobinHAEVG / binary_search.cs
Last active April 14, 2020 06:07
C# useful snippets
private static int SearchBinary(int[] array, int searchValue)
{
int min = 0;
int max = array.Length - 1;
while (min <= max)
{
int mid = (min + max) / 2;
if (searchValue == array[mid])
{
return mid;
@RobinHAEVG
RobinHAEVG / App.xaml
Last active January 28, 2020 09:22
C#/WPF MVVM Pattern Implementation using MvvmLight
<Application x:Class="ExampleProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ExampleProject.ViewModel"
d1p1:Ignorable="d"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
StartupUri="View/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
@RobinHAEVG
RobinHAEVG / get_request.cs
Created January 14, 2020 11:35
C# HTTP Snippets
//using System.Net;
using var wb = new WebClient();
var response = wb.DownloadString(url);
@RobinHAEVG
RobinHAEVG / Entity_JobEntry.cs
Last active February 3, 2020 14:10
C# MSSQL Connection
// NuGet package: EntityFramework from Microsoft
using System.ComponentModel.DataAnnotations;
public class JobEntry
{
[Key]
public int Id { get; set; }
[StringLength(50)]
public string UniqueId { get; set; }
[StringLength(100)]
@RobinHAEVG
RobinHAEVG / read_file_async.cs
Created February 3, 2020 13:39
Asynchronous C# File Operations
public static async Task<string> ReadAllTextAsync(string filePath)
{
var stringBuilder = new StringBuilder();
using (var fileStream = File.OpenRead(filePath))
using (var streamReader = new StreamReader(fileStream))
{
string line = await streamReader.ReadLineAsync();
while(line != null)
{
stringBuilder.AppendLine(line);
@RobinHAEVG
RobinHAEVG / ViewModelBase.cs
Created April 8, 2020 05:44
MVVM Event Propagation - Propagates property changes back to the view
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace ExampleProject
{
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = "")
{
@RobinHAEVG
RobinHAEVG / multitlsconfig.go
Last active October 19, 2020 12:00
Golang multiple TLS Config
package main
import (
"crypto/tls"
"log"
)
func main() {
var cert tls.Certificate // from wherever
tlsConfig := &tls.Config{
@RobinHAEVG
RobinHAEVG / embedded_file_helper.cs
Created October 30, 2020 12:01
Read embedded text-based files in C#
public static class Helper
{
private static readonly Dictionary<string, string> Cache = new Dictionary<string, string>();
public static string ReadUtf8EmbeddedRessource(string filename)
{
string utf8EmbeddedRessource;
lock (Cache)
{
@RobinHAEVG
RobinHAEVG / https-server.go
Last active April 21, 2021 10:39
Golang Secure HTTPS Server
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
},
}
@RobinHAEVG
RobinHAEVG / decode-utf16-to-utf8.go
Created June 11, 2021 07:31
Decode UTF16 LE/BE BOM to UTF8
func decodeUTF16ToUTF8(b []byte, bigEndian bool) ([]byte, error) {
if len(b) % 2 != 0 {
return nil, fmt.Errorf("must have even length byte slice")
}
var (
u16s = make([]uint16, 1)
ret = &bytes.Buffer{}
b8buf = make([]byte, 4)
)