Double To Int 64 Bits, Int 64 Bits To Double

image_pdfimage_print
   
 
// Portions copyright 2005 - 2007: Diego Guidi
// Portions copyright 2006 - 2008: Rory Plaire (codekaizen@gmail.com)
//
// This file is part of GeoAPI.
// GeoAPI is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// GeoAPI is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with GeoAPI; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

#if NETCF
using System;

namespace GeoAPI.DataStructures
{
    /// <summary>
    /// A supoort class: the purpose is to integrate System.BitConverter 
    /// methods not presents in .NET Compact Framework.
    /// </summary>
    public class BitConverter
    {
        public static Int64 DoubleToInt64Bits(Double x)
        {
#if UNSAFE
            unsafe
            {
                return *(Int64*)&amp;x;
            }
#else
            Byte[] bytes = System.BitConverter.GetBytes(x);
            Int64 value = System.BitConverter.ToInt64(bytes, 0);
            return value;
#endif
        }

        public static Double Int64BitsToDouble(Int64 x)
        {
#if UNSAFE
            unsafe
            {
                return *(Double*)&amp;x;
            }
#else
            Byte[] bytes = System.BitConverter.GetBytes(x);
            Double value = System.BitConverter.ToDouble(bytes, 0);
            return value;
#endif
        }
    }
}
#endif

   
     


Working with the Conditional Attribute

image_pdfimage_print
   
 


using System;
using System.Diagnostics;
   
public class TestClass
{
    public void Method1()
    {
        Console.WriteLine("Hello from Method1!");
    }
   
    [Conditional("DEBUG")]
    public void Method2()
    {
        Console.WriteLine("Hello from Method2!");
    }
   
    public void Method3()
    {
        Console.WriteLine("Hello from Method3!");
    }
   
}
   
class MainClass
{
    public static void Main()
    {
        TestClass MyTestClass = new TestClass();
   
        MyTestClass.Method1();
        MyTestClass.Method2();
        MyTestClass.Method3();
    }
}

    


Defensive Programming:Asserts

image_pdfimage_print
   


// compile with: csc /r:system.dll file_1.cs

using System;
using System.Diagnostics;

class MyClass
{
    public MyClass(int i)
    {
        this.i = i;
    }
    
    [Conditional("DEBUG")]
    public void VerifyState()
    {
        Debug.Assert(i == 0, "Bad State");
    }
    
    int i = 0;
}

public class DefensiveProgrammingAsserts
{
    public static void Main()
    {
        Debug.Listeners.Clear();
        Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
        MyClass c = new MyClass(1);
        
        c.VerifyState();
    }
}
           
          


Illustrates refusing permissions

image_pdfimage_print


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/


/*
  Example19_4.cs illustrates refusing permissions
*/

using System;
using System.IO;
using System.Security.Permissions;

[assembly:FileIOPermissionAttribute(SecurityAction.RequestRefuse,
Unrestricted=true)]

public class Example19_4 
{

    public static void Main() 
    {

        // Create a new file to work with
        FileStream fsOut = File.Create(@"c:	emp	est.txt");
        // Create a StreamWriter to handle writing
        StreamWriter sw = new StreamWriter(fsOut);
        // And write some data
        sw.WriteLine("&#039;Twas brillig, and the slithy toves");
        sw.WriteLine("Did gyre and gimble in the wabe.");
        sw.Flush();
        sw.Close();
    }
}
           
          


Illustrates requesting a permission set

image_pdfimage_print
   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example19_3.cs illustrates requesting a permission set
*/

using System;
using System.IO;
using System.Security.Permissions;

[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum,
 Name="FullTrust")]

public class Example19_3 
{

    public static void Main() 
    {

        // Create a new file to work with
        FileStream fsOut = File.Create(@"c:	emp	est.txt");
        // Create a StreamWriter to handle writing
        StreamWriter sw = new StreamWriter(fsOut);
        // And write some data
        sw.WriteLine("&#039;Twas brillig, and the slithy toves");
        sw.WriteLine("Did gyre and gimble in the wabe.");
        sw.Flush();
        sw.Close();

    }

}
           
          


Illustrates requesting optional permissions

image_pdfimage_print
   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example19_2.cs illustrates requesting optional permissions
*/

using System;
using System.IO;
using System.Security.Permissions;

[assembly:FileIOPermissionAttribute(SecurityAction.RequestOptional,
All=@"c:	emp")]

public class Example19_2 
{

    public static void Main() 
    {

        // Create a new file to work with
        FileStream fsOut = File.Create(@"c:	emp	est.txt");
        // Create a StreamWriter to handle writing
        StreamWriter sw = new StreamWriter(fsOut);
        // And write some data
        sw.WriteLine("&#039;Twas brillig, and the slithy toves");
        sw.WriteLine("Did gyre and gimble in the wabe.");
        sw.Flush();
        sw.Close();
    }
}