Math.Abs

image_pdfimage_print
   
  
using System;

public class MainClass {
    public static void Main() {

        Console.WriteLine("Math.Abs(5) = {0}", Math.Abs(5));
        Console.WriteLine("Math.Abs(-5) = {0}", Math.Abs(-5));
        Console.WriteLine("Math.Abs(0) = {0}", Math.Abs(0));
    }
}

   
     


Math.Sign

image_pdfimage_print
   
  
using System;

public class MainClass {
    public static void Main() {
        Console.WriteLine("Math.Sign(1) = {0}", Math.Sign(1));
        Console.WriteLine("Math.Sign(-1) = {0}", Math.Sign(-1));
        Console.WriteLine("Math.Sign(0) = {0}", Math.Sign(0));
    }
}

   
     


Use Math.Round

image_pdfimage_print
   
  

using System;

public class MainClass {
    public static void Main() {

        Console.WriteLine(Math.Round(4.4));
        Console.WriteLine(Math.Round(4.5));
        Console.WriteLine(Math.Round(4.6));
        Console.WriteLine(Math.Round(5.5));
        Console.WriteLine(Math.Round(4.54, 1));
        Console.WriteLine(Math.Round(4.55, 1));
        Console.WriteLine(Math.Round(4.65, 1));
        Console.WriteLine(Math.Round(4.56, 1));
    }
}

   
     


Math operators

image_pdfimage_print
   
  

using System;
class MathOpsApp
{
    static void Main(string[] args)
    {
        Random rand = new Random();
        int a, b, c;
   
        a = rand.Next() % 100; // Limit max to 99.
        b = rand.Next() % 100; // Limit max to 99.
   
        Console.WriteLine("a={0} b={1}", a, b);
   
        c = a * b;
        Console.WriteLine("a * b = {0}", c);
   
        c = a / b;
        Console.WriteLine("a / b = {0}", c);
   
        c = a + b;
        Console.WriteLine("a + b = {0}", c);
   
        c = a - b;
        Console.WriteLine("a - b = {0}", c);
   
        c = a % b;
        Console.WriteLine("a % b = {0}", c);
    }
}

   
     


MatchEvaluator: Entry Point IP Reverse

image_pdfimage_print


   


using System;
using System.Text;
using System.Text.RegularExpressions;

public class EntryPoint
{
    static void Main( string[] args ) {

        // Create regex to search for IP address pattern.
        string pattern = @"(?<part1>[01]?dd?|2[0-4]d|25[0-5])." +
                         @"(?<part2>[01]?dd?|2[0-4]d|25[0-5])." +
                         @"(?<part3>[01]?dd?|2[0-4]d|25[0-5])." +
                         @"(?<part4>[01]?dd?|2[0-4]d|25[0-5])";
        Regex regex = new Regex( pattern );
        Match match = regex.Match( "192.168.169.1" );

        MatchEvaluator eval = new MatchEvaluator(EntryPoint.IPReverse );
        Console.WriteLine( regex.Replace("192.168.169.1", eval) );
    }

    static string IPReverse( Match match ) {
        Console.WriteLine( match.Groups["part4"] + "." );
        Console.WriteLine( match.Groups["part3"] + "." );
        Console.WriteLine( match.Groups["part2"] + "." );
        Console.WriteLine( match.Groups["part1"] );
        return "";
    }
}

           
          


Define multiline patterns

image_pdfimage_print


   

using System;
using System.Text.RegularExpressions;

public class EntryPoint
{
    static void Main( string[] args ) {
        // Create regex to search for IP address pattern.
        string pattern = @"
# First part match
([01]?dd?         
# OR
 |2[0-4]d          
# OR
 |25[0-5])          
.                  

# REPEAT
([01]?dd?|2[0-4]d|25[0-5]).

# REPEAT
([01]?dd?|2[0-4]d|25[0-5]).

# REPEAT
([01]?dd?|2[0-4]d|25[0-5])
";
        Regex regex = new Regex( pattern,
                       RegexOptions.IgnorePatternWhitespace );
        Match match = regex.Match( "192.168.169.1" );
        while( match.Success ) {
            Console.WriteLine( "IP Address found at {0} with " +
                               "value of {1}",
                               match.Index,
                               match.Value );

            match = match.NextMatch();
        }
        
    }
}


           
          


Match Groups

image_pdfimage_print


   


using System;
using System.Text.RegularExpressions;

public class EntryPoint
{
    static void Main( string[] args ) {
        // Create regex to search for IP address pattern.
        string pattern = @"(?<part1>[01]?dd?|2[0-4]d|25[0-5])." +
                         @"(?<part2>[01]?dd?|2[0-4]d|25[0-5])." +
                         @"(?<part3>[01]?dd?|2[0-4]d|25[0-5])." +
                         @"(?<part4>[01]?dd?|2[0-4]d|25[0-5])";
        Regex regex = new Regex( pattern );
        Match match = regex.Match( "192.168.169.1" );
        while( match.Success ) {
            Console.WriteLine( "IP Address found at {0} with " +
                               "value of {1}",
                               match.Index,
                               match.Value );
            Console.WriteLine( "Groups are:" );
            Console.WriteLine( "	Part 1: {0}",
                               match.Groups["part1"] );
            Console.WriteLine( "	Part 2: {0}",
                               match.Groups["part2"] );
            Console.WriteLine( "	Part 3: {0}",
                               match.Groups["part3"] );
            Console.WriteLine( "	Part 4: {0}",
                               match.Groups["part4"] );

            match = match.NextMatch();
        }
        
    }
}