C#. Sync and Async simple examples
static void SyncExample() { // do smth before var fs = new FileStream(); // wait for this operation // do smth after } static
static void SyncExample() { // do smth before var fs = new FileStream(); // wait for this operation // do smth after } static
https://leetcode.com/problems/buddy-strings/
/* main idea is to search unmatched symbols, and it shoeld be only 2 of them to swap */ public class Solution { public
https://leetcode.com/problems/add-strings/
public string AddStrings(string num1, string num2) { var needZeroes = num1.Length — num2.Length; // adding leading zeroes var absNeedZeroes = Math.Abs(needZeroes); if (needZeroes
isomorphing strings on leetcode
public bool IsIsomorphic(string s, string t) { if (s.Length != t.Length) return false; var firstStringDict = new Dictionary<char,
https://leetcode.com/problems/valid-palindrome/
my decision below
static bool IsPalindrome(string s) { var alphaNum = GetAlphaNumFromString(s).ToLower(); if (alphaNum == «») return true; var reversed = ReverseString(alphaNum); Console.WriteLine(alphaNum);
Promise.all is all or nothing, it returns promise if all incoming promises resolved
const myPromiseAll = (promises) => { return new Promise((resolve, reject) => {
simple example, task will be created periodically and will be handled in thread pool with Thread.QueueUserWorkItem
var timer = new Timer( (o) => {
simple example
int[] nums = Enumerable.Range(1, 10).ToArray(); var res = nums.AsParallel().Where(n => n < 5).ToArray(); Thread.Sleep(2000); Console.WriteLine(String.Join(» «, res));
output
1 2 3 4
tasks that can be done in parallel
static void Main(string[] args) { Parallel.For(1, 10, i => DoWork(i)); Thread.Sleep(12000); } static void DoWork(int i) {
static void Main(string[] args) { Task<int> t = new Task<int>(() => { Console.WriteLine(«smth in thread»); return 123; }); Console.WriteLine(t.Status); t.Start(); Console.WriteLine(t.Status); Thread.Sleep(3000); Console.WriteLine(t.Status); }