Wednesday, August 26, 2020

C# - Extension Method For ForEach

ForEach Extension for any arbitrary IEnumerable<T> sequence:

// Ex Usage
yourSequence.ForEach(x => Console.WriteLine(x)); // Extension Method public static class EnumerableExtensions { public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { if (source == null) throw new ArgumentNullException("source"); if (action == null) throw new ArgumentNullException("action"); foreach (T item in source) { action(item); } } }

If you're dealing with an array then you can use the built-in static ForEach method:

Array.ForEach(yourArray, x => Console.WriteLine(x));

If you're dealing with a List<T> then you can use the built-in ForEach instance method:

yourList.ForEach(x => Console.WriteLine(x));

Friday, May 15, 2020

Powershell - Test if TCP port is open on Win server

Test-NetConnection -Port 801 -ComputerName 192.168.1.1 -InformationLevel Detailed


Friday, May 8, 2020

LINQ, LAMBDA - Example of Difference between Select and SelectMany


using System;
using System.Linq;


class School
{
 public string Name{get; set;}
 public Student[] Students{get;set;}
}

class Student 
{
 public string Name {get;set;}
}

     
public class Program
{
 public static void Main()
 {
  
  var schools = new [] {
   new School(){Name ="Harvard", Students = new [] { new Student(){ Name="Bob"}, new Student(){ Name="Jack"} }},
   new School(){Name ="Stanford", Students = new [] { new Student(){ Name="Jim"}, new Student(){ Name="John"} }}
  };
  
  //Select Many
  var allStudents = schools.SelectMany(s=> s.Students);
  
  Console.WriteLine("Results with one loop and SelectMany\n");
  foreach(var student in allStudents){
   Console.WriteLine(student.Name);
  }
  
  // Select
  var mySchool = schools.Select(s => s.Students);
  
  Console.WriteLine("Results with two loops and Select\n");
  foreach(var school in mySchool){

   foreach(var studentd in school){
    Console.WriteLine(studentd.Name);
   }
   
  }
  
 }
}

Thursday, April 23, 2020

URL, C# - Get Url Query Param


//Get The Hole URL   
Uri theRealUrl = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);

                    string aliasFromUrl = HttpUtility.ParseQueryString(theRealUrl.Query).Get("alias");

Friday, March 20, 2020