//----------------------------------------------------------------------- // <copyright file="Extensions.cs" company="Microsoft Corporation copyright 2008."> // (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. // </copyright> // <date>26-Feb-2009</date> // <summary>Extensions class.</summary> //----------------------------------------------------------------------- namespace Blacklight.Controls { using System; using System.Windows; using System.Windows.Media; /// <summary> /// This set of internal extension methods provide general solutions and /// utilities in a small enough number to not warrant a dedicated extension /// methods class. /// </summary> internal static partial class Extensions { /// <summary> /// Inverts a Matrix. The Invert functionality on the Matrix type is /// internal to the framework only. Since Matrix is a struct, an out /// parameter must be presented. /// </summary> /// <param name="m">The Matrix object.</param> /// <param name="outputMatrix">The matrix to return by an output /// parameter.</param> /// <returns>Returns a value indicating whether the type was /// successfully inverted. If the determinant is 0.0, then it cannot /// be inverted and the original instance will remain untouched.</returns> public static bool Invert(this Matrix m, out Matrix outputMatrix) { double determinant = m.M11 * m.M22 - m.M12 * m.M21; if (determinant == 0.0) { outputMatrix = m; return false; } Matrix matCopy = m; m.M11 = matCopy.M22 / determinant; m.M12 = -1 * matCopy.M12 / determinant; m.M21 = -1 * matCopy.M21 / determinant; m.M22 = matCopy.M11 / determinant; m.OffsetX = (matCopy.OffsetY * matCopy.M21 - matCopy.OffsetX * matCopy.M22) / determinant; m.OffsetY = (matCopy.OffsetX * matCopy.M12 - matCopy.OffsetY * matCopy.M11) / determinant; outputMatrix = m; return true; } /// <summary> /// An implementation of the Contains member of string that takes in a /// string comparison. The traditional .NET string Contains member uses /// StringComparison.Ordinal. /// </summary> /// <param name="s">The string.</param> /// <param name="value">The string value to search for.</param> /// <param name="comparison">The string comparison type.</param> /// <returns>Returns true when the substring is found.</returns> public static bool Contains(this string s, string value, StringComparison comparison) { return s.IndexOf(value, comparison) >= 0; } } }
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]
Unsigned long value
using System; public class MainClass{ public static void Main() { unsigned long ulVal = 0776745ul; // Unsigned long value Console.WriteLine(ulVal); } }
Demonstate automatic conversion from long to double
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstate automatic conversion from long to double. using System; public class LtoD { public static void Main() { long L; double D; L = 100123285L; D = L; Console.WriteLine("L and D: " + L + " " + D); } }
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 ); } }