Display value using the standard format specifiers for ulong value

   
 

using System;

public class Example
{
   public static void Main()
   {
      ulong value = 163249057;
      // Display value using default ToString method.
      Console.WriteLine(value.ToString());      
      Console.WriteLine();

      // Define an array of format specifiers.
      string[] formats = { "G", "C", "D", "F", "N", "X" };
      // Display value using the standard format specifiers.
      foreach (string format in formats)
         Console.WriteLine("{0} format specifier: {1,16}",format, value.ToString(format));         
   }
}

   
     


Convert long value to KB,MB,GB,TB

//Microsoft Reciprocal License (Ms-RL)
//http://bmcommons.codeplex.com/license
using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace BlueMirror.Commons
{

public static class IntExtensions
{
public static string ToRoman(this int number, bool upperCase) {
return CustomConvert.ToRoman(number, upperCase);
}
}

public static class CustomConvert
{
public static string ToRoman(int number, bool upperCase)
{
if (number < 0) throw new ArgumentOutOfRangeException("number", number, "Liczba musi byæ wiêksza od zera."); string[] romans = new string[] {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"}; // string[] romansLower = new string[] {"i", "iv", "v", "ix", "x", "xl", "l", "xc", "c", "cd", "d", "cm", "m"}; int[] numbers = new int[] {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000}; int j = 12; string result = ""; // string[] romans = upperCase? romansUpper: romansLower; // za romansUpper i romansLower powstawiac konstruktory tablic - new string[] {} while(number != 0) { if(number >= numbers[j])
{
number -= numbers[j];
result += romans[j];
}
else
j–;
}
if (!upperCase)
result = result.ToLower();
return result;
}

public static string ToSay(double number)
{
double floor = Math.Floor(number);
string result = ToSay(System.Convert.ToInt64(floor)) + ” i ” + ToSay(System.Convert.ToInt64(Math.Round((number – floor) * 100))) + ” setnych”;
return result;
}

public static string ToSay(decimal number, IFormatProvider format)
{
string result;
NumberFormatInfo nfi;
if(format != null)
nfi = (NumberFormatInfo)format.GetFormat(typeof(NumberFormatInfo));
else
nfi = CultureInfo.CurrentCulture.NumberFormat;
//if (nfi == null)
// throw new Exception(“Nie mo¿na uzyskaæ obiektu NumberFormatInfo.”);
// TODO: double i decimal – wyprostowaæ.
double floor = Math.Floor((double)number);
//long floor = System.Convert.ToInt64(Math.Floor(number));
result = ToSay(System.Convert.ToInt64(floor)) + ” ” + nfi.CurrencySymbol + ” ” + ToSay(System.Convert.ToInt64(Math.Round(((double)number – floor) * 100)));
return result;
}

public static string ToKB(long bytes) {
string[] suffix = new string[] { “B”, “KB”, “MB”, “GB”, “TB” };
float byteNumber = bytes;
for (int i = 0; i < suffix.Length; i++) { if (byteNumber < 1000) if(i == 0) return string.Format("{0} {1}", byteNumber, suffix[i]); else return string.Format("{0:0.#0} {1}", byteNumber, suffix[i]); else byteNumber /= 1024; } return string.Format("{0:N} {1}", byteNumber, suffix[suffix.Length - 1]); } public static string ToRegex(string wildcard) { string result = "^"; foreach (char chin in wildcard) { if (chin == '*') result += ".*"; else if (chin == '?') result += "."; else if (chin == ';') result += "$|^"; else if ("+()^$.{}[]|".IndexOf(chin) != -1) result += "" + chin; else result += chin; } return result + "$"; } } } [/csharp]

Compute the distance from the Earth to the sun, in inches.


   
  
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Compute the distance from the Earth to the sun, in inches. 
 
using System; 
 
public class Inches {    
  public static void Main() {    
    long inches; 
    long miles; 
   
    miles = 93000000; // 93,000,000 miles to the sun 
 
    // 5,280 feet in a mile, 12 inches in a foot 
    inches = miles * 5280 * 12; 
   
    Console.WriteLine("Distance to the sun: " + 
                      inches + " inches."); 
   
  }    
}


           
         
    
     


Creating a literal


   



using System;

class Test {

   public static void Main() {
     // Create a simple string literal
     string s1 = "This is a test";

     // Create a string literal with an escaped character
     string s2 = "This is a "real" test";

     // Create a string literal with the @ sign
     string s3 = @"This is a 
eal test";

     // Output them
     Console.WriteLine("String 1 = {0}", s1 );
     Console.WriteLine("String 2 = {0}", s2 );
     Console.WriteLine("String 3 = {0}", s3 );
  }
}
           
          


String literals


   


using System;

public class EntryPoint
{
    static void Main( string[] args ) {
        string lit1 = "c:windowssystem32";
        string lit2 = @"c:windowssystem32";

        string lit3 = @"
line one
line two
";
        Console.WriteLine( lit3 );

        Console.WriteLine( "Object.RefEq(lit1, lit2): {0}",
                           Object.ReferenceEquals(lit1, lit2) );

    }
}