Implicit cast

image_pdfimage_print
   
 

using System;
class ImplicitConversion {
    public static void Main() {
        byte a = 1;
        int b = 1234;
        int c = a; //Implicit cast
        double d = b; //Implicit cast
        Console.WriteLine("{0}", c);
        Console.WriteLine("{0}", d);
    }
}

    


Any object, value, or reference type that is convertible to an integral, char, enum, or string type is acceptable as the switch_expression

image_pdfimage_print
   
 

using System;
class Employee {
    public Employee(string f_Emplid) {
        m_Emplid = f_Emplid;
    }

    static public implicit operator string(Employee f_this) {
        return f_this.m_Emplid;
    }

    private string m_Emplid;
}

class Starter {
    static void Main() {
        Employee newempl = new Employee("1234");
        switch (newempl) {
            case "1234":
                Console.WriteLine("Employee 1234");
                return;
            case "5678":
                Console.WriteLine("Employee 5678");
                return;
            default:
                Console.WriteLine("Invalid employee");
                return;
        }
    }
}

    


Conversions between Simple Types

image_pdfimage_print
   
 

using System;

public class MainClass {
    public static void Main() {

        double d = 2.9;
        Console.WriteLine((int)d);                   // double-->int; prints 2 
        Console.WriteLine((int)(-d));                // double-->int; prints -2 
        uint seconds = (uint)(24 * 60 * 60);         // int-->uint 
        double avgSecPerYear = 365.25 * seconds;     // I uint-->double 
        float f = seconds;                           // IL uint-->float 
        long nationalDebt1 = 99999999999999;
        double perSecond = 99999.14;
        decimal perDay = seconds * (decimal)perSecond;              // I uint-->decimal 
        double nd2 = nationalDebt1 + (double)perDay; // decimal-->double 
        long nd3 = (long)nd2;                        // double-->long 
        float nd4 = (float)nd2;                      // double-->float 

    }

}

    


Type convert

image_pdfimage_print
   
 
/*
Type          Can Safely Be Converted To

Byte          short, ushort, int, uint, long, ulong, float, double, decimal

Sbyte         short, int, long, float, double, decimal

Short         int, long, float, double, decimal

Ushort        int, uint, long, ulong, float, double, decimal

Int           long, float, double, decimal

Uint          long, ulong, float, double, decimal

Long          float, double, decimal

Ulong         float, double, decimal

Float         double

Char          ushort, int, uint, long, ulong, float, double, decimal 
*/

    


Byte array Index Of

image_pdfimage_print

//GNU General Public License version 2 (GPLv2)
//http://walkmen.codeplex.com/license

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

namespace Walkmen.Util
{
public sealed class ByteUtils
{
private ByteUtils()
{
}

public static int IndexOf(byte[] array, byte[] pattern, int offset)
{
int success = 0;
for (int i = offset; i < array.Length; i++) { if (array[i] == pattern[success]) { success++; } else { success = 0; } if (pattern.Length == success) { return i - pattern.Length + 1; } } return -1; } } } [/csharp]

Write int 32 to byte array

image_pdfimage_print
   
 
//http://walkmen.codeplex.com/license
// License:  GNU General Public License version 2 (GPLv2)  


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

namespace Walkmen.Util
{
    public sealed class NumericUtils
    {
        private NumericUtils()
        {
        }

        public static byte[] WriteInt32(int value)
        {
            byte[] result = new byte[4];
            result[0] = (byte)((value &amp; 0xFF000000) >> 24);
            result[1] = (byte)((value &amp; 0x00FF0000) >> 16);
            result[2] = (byte)((value &amp; 0x0000FF00) >> 8);
            result[3] = (byte)((value &amp; 0x000000FF));

            return result;
        }
   }
}

   
     


Read int 32 from byte array

image_pdfimage_print

//http://walkmen.codeplex.com/license
// License: GNU General Public License version 2 (GPLv2)

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

namespace Walkmen.Util
{
public sealed class NumericUtils
{
private NumericUtils()
{
}

public static int ReadInt32(byte[] buffer, int offset)
{
return (buffer[offset + 0] << 24) + (buffer[offset + 1] << 16) + (buffer[offset + 2] << 8) + (buffer[offset + 3]); } } } [/csharp]