using System; public class PassArguments { public static void Main(String[] args) { double hotC, coldC; double hotF, coldF; if (args.Length != 2) { Console.WriteLine ("Enter a hot and a cold temerature as program arguments."); return; } hotC = double.Parse(args[0]); hotF = 9.0*hotC/5.0 + 32.0; Console.WriteLine ("The Fahrenheit temperature is: {0:F1}", hotF); coldC = double.Parse(args[1]); coldF = 9.0*coldC/5.0 + 32.0; Console.WriteLine ("The Fahrenheit temperature is: {0:F1}", coldF); } }
Development Class
To Four Digit Year
using System; using System.Globalization; class Class1 { static void Main(string[] args) { Calendar MyCalendar = new GregorianCalendar(); Console.WriteLine(MyCalendar.ToFourDigitYear(32)); Console.WriteLine(MyCalendar.ToFourDigitYear(28)); } }
Two Digit Year Max
using System; using System.Globalization; class Class1 { static void Main(string[] args) { Calendar MyCalendar = new GregorianCalendar(); CultureInfo MyCulture = new CultureInfo("es-ES"); DateTime MyDate = new DateTime(2006,8,22,15,30,0,0); Console.WriteLine(MyCalendar.TwoDigitYearMax); } }
ToString(): make it culture aware
using System; using System.Globalization; class Class1 { static void Main(string[] args) { Calendar MyCalendar = new GregorianCalendar(); CultureInfo MyCulture = new CultureInfo("es-ES"); DateTime MyDate = new DateTime(2006,8,22,15,30,0,0); Console.WriteLine(MyCalendar.AddMinutes(MyDate, 15). ToString("G", MyCulture)); } }
Calendar ToDateTime is not culture aware
using System; using System.Globalization; class Class1 { static void Main(string[] args) { Calendar MyCalendar = new GregorianCalendar(); CultureInfo MyCulture = new CultureInfo("es-ES"); DateTime MyDate = new DateTime(2006,8,22,15,30,0,0); // Console.WriteLine(MyCalendar.ToDateTime(MyDate.Year, MyDate.Month, MyDate.Day, MyDate.Hour, MyDate.Minute, 0, 0)); } }
Provide a memory caching mechanism to save and retrieve results based on the associated key names
// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.Text; namespace crudwork.Utilities { /// <summary> /// Provide a memory caching mechanism to save and retrieve results based on the associated key names /// </summary> /// <typeparam name="T">the key</typeparam> /// <typeparam name="U">the result</typeparam> public class CacheManager<T, U> { private Dictionary<T, U> cache; /// <summary> /// Create an empty instance /// </summary> public CacheManager() { cache = new Dictionary<T, U>(); } /// <summary> /// Generate a key based on the input provided. /// </summary> /// <param name="keys"></param> /// <returns></returns> public string MakeKey(params string[] keys) { if (keys == null || keys.Length == 0) throw new ArgumentNullException("keys"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < keys.Length; i++) { if (i > 0) sb.Append("ƒ"); // delimiter by ASCII 159 sb.Append(keys[i].ToUpper().Trim(' ', ' ')); } return sb.ToString(); } /// <summary> /// Check whether or not the result exists in the cache by the associated key /// </summary> /// <param name="key"></param> /// <returns></returns> public bool Exists(T key) { return cache.ContainsKey(key); } /// <summary> /// Retrieve the result from cache by the associated key /// </summary> /// <param name="key"></param> /// <returns></returns> public U Get(T key) { return cache[key]; } /// <summary> /// Add the result/key pair to the cache /// </summary> /// <param name="key"></param> /// <param name="value"></param> public void Add(T key, U value) { if (cache.ContainsKey(key)) cache[key] = value; else cache.Add(key, value); } /// <summary> /// Remove the result from cache by the associated key /// </summary> /// <param name="key"></param> public void Delete(T key) { cache.Remove(key); } /// <summary> /// Clear all cache from memory /// </summary> public void Clear() { cache.Clear(); } /// <summary> /// Load cache entries from given file /// </summary> /// <param name="filename"></param> public void Load(string filename) { throw new NotImplementedException("not done"); } /// <summary> /// Save all cache entries to given file /// </summary> /// <param name="filename"></param> public void Save(string filename) { throw new NotImplementedException("not done"); } } }
Provides efficient storage for cached items.
//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Redwerb.BizArk.Core.Util
{
///
///
public class Cache
{
#region Initialization and Destruction
///
///
public Cache()
{
// The default expiration time is 5 minutes.
DefaultExpiration = new TimeSpan(0, 5, 0);
}
#endregion
#region Fields and Properties
private Dictionary
///
///
///
///
public object this[string key]
{
get { return GetValue(key); }
set { SetValue(key, value, DefaultExpiration); }
}
///
///
public TimeSpan DefaultExpiration { get; set; }
#endregion
#region Methods
///
///
///
///
public void SetValue(string key, object value)
{
SetValue(key, value, DefaultExpiration);
}
///
///
///
///
///
public void SetValue(string key, object value, TimeSpan expiration)
{
if (mItems.ContainsKey(key))
mItems.Remove(key);
// We don't store nulls.
if (value == null) return;
mItems.Add(key, new CacheItem(key, value, DateTime.Now.Add(expiration)));
}
///
///
///
public void ClearValue(string key)
{
SetValue(key, null);
}
///
///
///
///
public object GetValue(string key)
{
if (mItems.ContainsKey(key))
{
var item = mItems[key];
if (item.HasExpired)
return null;
else
return item.Value;
}
else
return null;
}
///
///
///
///
///
///
public T GetValue
{
var value = GetValue(key);
if (value == null)
return defaultVal;
else
return (T)value;
}
///
///
public void PurgeCache()
{
var items = mItems.Values;
foreach (var item in items)
{
if (item.HasExpired)
mItems.Remove(item.Key);
}
}
///
///
public void ClearCache()
{
mItems.Clear();
}
#endregion
#region CacheItem
private class CacheItem
{
public CacheItem(string key, object value, DateTime expirationDate)
{
Key = key;
ExpirationDate = expirationDate;
mValRef = new WeakReference(value);
}
private WeakReference mValRef;
public string Key { get; private set; }
///
///
public object Value
{
get
{
if (mValRef.IsAlive)
return mValRef.Target;
else
return null;
}
}
public DateTime ExpirationDate { get; private set; }
public bool HasExpired
{
get
{
if (!mValRef.IsAlive)
return false;
else if (DateTime.Now < ExpirationDate)
return false;
else
return true;
}
}
}
#endregion
}
}
[/csharp]