Comparing integers using if statements, equality operators, and relational operators.

image_pdfimage_print
   
 

       

using System;

public class Comparison
{
   public static void Main( string[] args )
   {
      int number1; 
      int number2; 

      Console.Write( "Enter first integer: " );
      number1 = Convert.ToInt32( Console.ReadLine() );

      Console.Write( "Enter second integer: " );
      number2 = Convert.ToInt32( Console.ReadLine() );

      if ( number1 == number2 )
         Console.WriteLine( "{0} == {1}", number1, number2 );

      if ( number1 != number2 )
         Console.WriteLine( "{0} != {1}", number1, number2 );

      if ( number1 < number2 )
         Console.WriteLine( "{0} < {1}", number1, number2 );

      if ( number1 > number2 )
         Console.WriteLine( "{0} > {1}", number1, number2 );

      if ( number1 <= number2 )
         Console.WriteLine( "{0} <= {1}", number1, number2 );

      if ( number1 >= number2 )
         Console.WriteLine( "{0} >= {1}", number1, number2 );
   } 
}
     
         
     


Calculating the product of three integers.

image_pdfimage_print
   
 

using System;

public class Product {
    public static void Main(string[] args) {
        int x;
        int y;
        int z;
        int result;

        Console.Write("Enter first integer: ");
        x = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter second integer: ");
        y = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter third integer: ");
        z = Convert.ToInt32(Console.ReadLine());

        result = x * y * z;

        Console.WriteLine("Product is {0}", result);
    }
}
           
         
     


Call methods from primitive data types

image_pdfimage_print
   
  

using System;
using System.Collections.Generic;
using System.Text;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(12.GetHashCode());
        Console.WriteLine(12.Equals(23));
        Console.WriteLine(12.GetType().BaseType);
        Console.WriteLine(12.ToString());
        Console.WriteLine(12); // ToString() called automatically.

    }
}

   
     


Check IndexOutOfRangeException

image_pdfimage_print
   
 

using System;
public class MainEntryPoint {
    public static void Main() {
        string userInput;
        while (true) {
            try {
                Console.Write("Input a number between 0 and 5 " +
                   "(or just hit return to exit)> ");
                userInput = Console.ReadLine();
                if (userInput == "")
                    break;
                int index = Convert.ToInt32(userInput);
                if (index < 0 || index > 5)
                    throw new IndexOutOfRangeException("You typed in " + userInput);
                Console.WriteLine("Your number was " + index);
            } catch (IndexOutOfRangeException e) {
                Console.WriteLine("Exception: Number should be between 0 and 5. " + e.Message);
            } catch (Exception e) {
                Console.WriteLine("An exception was thrown. Message was: " + e.Message);
            } catch {
                Console.WriteLine("Some other exception has occurred");
            } finally {
                Console.WriteLine("Thank you");
            }
        }
    }
}

    


Convert Hex char To Int

image_pdfimage_print

#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace Newtonsoft.Json.Utilities
{
internal class MathUtils
{

public static int HexToInt(char h)
{
if ((h >= '0') && (h <= '9')) { return (h - '0'); } if ((h >= 'a') && (h <= 'f')) { return ((h - 'a') + 10); } if ((h >= 'A') && (h <= 'F')) { return ((h - 'A') + 10); } return -1; } } } [/csharp]

Hex Translator

image_pdfimage_print

//http://www.bouncycastle.org/
//MIT X11 License

using System;

namespace Org.BouncyCastle.Utilities.Encoders
{
///

/// A hex translator.
///

public class HexTranslator
{
private static readonly byte[] hexTable =
{
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
};

///

/// Return encoded block size.
///

/// 2
public int GetEncodedBlockSize()
{
return 2;
}

///

/// Encode some data.
///

/// Input data array. /// Start position within input data array. /// The amount of data to process. /// The output data array. /// The offset within the output data array to start writing from. /// Amount of data encoded.
public int Encode(
byte[] input,
int inOff,
int length,
byte[] outBytes,
int outOff)
{
for (int i = 0, j = 0; i < length; i++, j += 2) { outBytes[outOff + j] = hexTable[(input[inOff] >> 4) & 0x0f];
outBytes[outOff + j + 1] = hexTable[input[inOff] & 0x0f];

inOff++;
}

return length * 2;
}

///

/// Returns the decoded block size.
///

/// 1
public int GetDecodedBlockSize()
{
return 1;
}

///

/// Decode data from a byte array.
///

/// The input data array. /// Start position within input data array. /// The amounty of data to process. /// The output data array. /// The position within the output data array to start writing from. /// The amount of data written.
public int Decode(
byte[] input,
int inOff,
int length,
byte[] outBytes,
int outOff)
{
int halfLength = length / 2;
byte left, right;
for (int i = 0; i < halfLength; i++) { left = input[inOff + i * 2]; right = input[inOff + i * 2 + 1]; if (left < (byte)'a') { outBytes[outOff] = (byte)((left - '0') << 4); } else { outBytes[outOff] = (byte)((left - 'a' + 10) << 4); } if (right < (byte)'a') { outBytes[outOff] += (byte)(right - '0'); } else { outBytes[outOff] += (byte)(right - 'a' + 10); } outOff++; } return halfLength; } } } [/csharp]