Monday, 23 November 2015

Simple Use of ThreadStart Delegate in C#

using System;
using System.Threading;
class Program
{
    static void Main()
    {
        Thread t = new Thread(new ThreadStart(Test.DemoMethod));
        t.Start();
    }
}
class Test
{
    public static void DemoMethod()
    {
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine(i);
        }
    }
}

Simple Threading in C#

using System;
using System.Threading;
class Program
{
    static void Main()
    {
        Thread t = new Thread(Test.DemoMethod);
        t.Start();
    }
}
class Test
{
    public static void DemoMethod()
    {
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine(i);
        }
    }
}

Wednesday, 4 November 2015

Verbatim String in C#

A String can be created as a verbatim string using @ symbol. Verbatim strings start with @ symbol. The C# compiler understand this type of string as verbatim. Basically the @ symbol tells the string constructor to ignore escape characters and line breaks.


using System;
class Program
{
    static void Main()
    {
        string str1 = "One\nTwo\nThree\nFour\nFive";
        //In statement above the compiler understand escape sequence', but if we write the code like this:

        string str2 = @"One\nTwo\nThree\nFour\nFive";
        //In statement above the compiler ignores escape sequence.

        Console.WriteLine(str1);
        Console.WriteLine("\n");
        Console.WriteLine(str2);
    }
}

Output:

One
Two
Three
Four
Five


One\nTwo\nThree\nFour\nFive



Saturday, 31 October 2015

Write a Program in C# to Validate Mobile Number using Regular Expressions.

using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main()
    {
        Console.WriteLine("Enter your mobile number");
        string mobileNumber = Console.ReadLine();

        string MNregExp = @"^\+?\d{0,2}\-?\d{4,5}\-?\d{5,6}";

        if (Regex.IsMatch(mobileNumber, MNregExp))
        {
            Console.WriteLine("Mobile number is in correct format");
        }
        else
        {
            Console.WriteLine("Mobile number is in incorrect format");
        }
    }
}