using System; public class MainClass{ public static void Main() { unsigned long ulVal = 0776745ul; // Unsigned long value Console.WriteLine(ulVal); } }
Month: February 2011
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 ); } }
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) ); } }
Output with x8 format
using System; class BWC { static void Main() { int[] values = { 0, 0x111, 0xfffff, 0x8888, 0x22000022 }; foreach (int v in values) { Console.WriteLine("~0x{0:x8} = 0x{1:x8}", v, ~v); } } }
Get Ordinal
#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.Globalization;
namespace Newtonsoft.Utilities.Text
{
public class FormatUtils
{
public static string GetOrdinal(int value)
{
string[] _suffixes = new string[] { “th”, “st”, “nd”, “rd” };
int tenth = value%10;
if (tenth >= _suffixes.Length)
{
return _suffixes[0];
}
else
{
// special case for 11, 12, 13
int hundredth = value % 100;
if (hundredth >= 11 && hundredth <= 13)
return _suffixes[0];
return _suffixes[tenth];
}
}
}
}
[/csharp]