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)); } }
Author: coder
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]
Throw Argument Out Of Range Exception
using System; using System.Collections; class Class1 { static void Main(string[] args) { try { // Display the first 200 chars from the connection string Console.WriteLine("asdf".Substring(1, 200)); } catch (Exception objE) { Console.WriteLine(objE.ToString()); } } }
Argument Null Exception
using System; class ArgumentNullTest { public static void Main() { String[] s = null; String sep = " "; try { String j = String.Join(sep,s); } catch (ArgumentNullException e) { Console.WriteLine("Error: {0}",e); } } }
Throw Invalid Operation Exception
using System; using System.Collections; public class InvalidOpExample { public static void Main() { int[] array = {0,0}; IEnumerator enumerator = array.GetEnumerator(); Console.Write("{0}",enumerator.Current); } }
Throw Null reference Exception
using System; using System.Collections; class Class1 { static void Main(string[] args) { Exception objException = new Exception(); try { objException = null; Console.WriteLine(objException.Message); } catch (Exception objE) { Console.WriteLine(objE.ToString()); } } }