c# snippets

parse string

reverse a string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
using System;

public class Program
{
    public static void Main()
    {
        string myString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] charArray = myString.ToCharArray();
        Array.Reverse(charArray);
        Console.Write(charArray);
    }
}

regex

example

static void Main(strong[] args)
{
string[] languages = {C#”, Python, Java};
foreach(string s in languages)
{
if(System.Text.RegularExpressions.Regex.IsMatch(s,C#”))
{
Console.WriteLine(Match found);
}
}
}

deadlocks

stack

static object object1 = new object();
static object object2 = new object();

public static void ObliviousFunction()
{
    lock (object1)
    {
        Thread.Sleep(1000); // Wait for the blind to lead
        lock (object2)
        {
        }
    }
}

public static void BlindFunction()
{
    lock (object2)
    {
        Thread.Sleep(1000); // Wait for oblivion
        lock (object1)
        {
        }
    }
}

static void Main()
{
    Thread thread1 = new Thread((ThreadStart)ObliviousFunction);
    Thread thread2 = new Thread((ThreadStart)BlindFunction);

simple fixes 🤦‍♂️

when you browse to a server you shouldn't see the iis default.html page

you can replace it with your web app index.html or you can redirect it in various ways

  • default.html

you can redirect the default iis page to your site

<script type="text/javascript">
<!--
    window.location = "http://mywebsite/virtualfolder"
//-->
</script>
  • yellow page of death

you should only see the yellow page of death on the server, end users don't need to see that noise lol

web.config

<configuration>
  <system.web>
    <customErrors defaultRedirect="yourErrorPage.html"
                  mode="RemoteOnly">

    </customErrors>
  </system.web>
</configuration>

Comments