1/// <summary>
/// 主题:委托基本操作实例
/// 描述:用于定义委托和构造方法
/// 作者:白宁超
/// </summary>
/// <summary>
/// 主题:委托类
/// 描述:用于定义委托和构造方法
/// 作者:白宁超
/// </summary>
public class myclass
{
public delegate void obj(string name);
public void objDelegateMsg(obj Method, string name)
{
Method("hello:" + name);
}
}
static void Main(string[] args)
{
//主题:StringBuilder类的操作,提高性能,多次使用避免建立多个对象
StringBuilder stb = new StringBuilder("hello");
myclass my = new myclass();//实例化类
my.objDelegateMsg(x => Console.WriteLine(x), "朋友!");//lambda表达式输出
Console.ReadKey(); //hello:朋友
}
利用委托自定义方法,每次调用只能调委托方法,可以带有多个参数,StringBuilder提高访问性能,如果多个对象访问同一资源,第二次以后直接从缓存中加载
2 //主题:格式转换
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("F"));//2013年1月5日 9:50:19
Console.WriteLine(dt.ToString("yyyy年MM月dd日hh时mm分ss秒") + "<br>");//2013年01月05日 9时50分19秒
Console.WriteLine(dt.ToString("ddd"));//周六
Console.ReadKey();
格式转化在一些特殊情况以特定格式输出时,则显得很重要了。
3//主题:集合元素遍历的时候,把集合锁定
static void Main(string[] args)
{
//主题:集合元素遍历的时候,把集合锁定
ArrayList al = new ArrayList();
al.Add("aaa");
al.Add("bbb");
al.Insert(0, "cc");//指定插入位置
lock (al.SyncRoot) // SyncRoot摘要: 获取可用于同步 System.Collections.ArrayList 访问的对象。
{
for (int i = 0; i < al.Count; i++)
{
foreach (string item in al)
{
Console.WriteLine(item);
}
}
}
Console.ReadKey();
}
集合可以指定位置添加,排序,指定范围等多个操作
4 //主题:应用程序域和程序集操作
//作者:白宁超
AppDomain domain = AppDomain.CreateDomain("MyDomain");//创建应用程序域
Console.WriteLine(" " + AppDomain.CurrentDomain.FriendlyName); //显示当前应用程序域的友好名称
Console.WriteLine(" " + domain.FriendlyName);//显示新建应用程序域的友好名称
Console.ReadKey();
static void Main(string[] args)
{
//创建与卸载应用程序域
Console.WriteLine("创建一个应用程序域:");
AppDomain domain = AppDomain.CreateDomain("MyDomain", null);
Console.WriteLine("主机程序域: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("新建程序域: " + domain.FriendlyName);
Console.ReadKey();
// 卸载应用程序域
AppDomain.Unload(domain);
Console.WriteLine("主机程序域: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("新建程序域: " + domain.FriendlyName); //下面这行代码会产生异常,因为domain对象被卸载了
Console.ReadKey();
}
// 将程序集加载到应用程序域中
// 将程序集加载到应用程序域中
static void Main(string[] args)
{
AppDomain ad = AppDomain.CurrentDomain;//取得当前应用程序域
Assembly a = ad.Load("~/Microsoft.Silverlight.ServiceReference.dll"); //加载ClassLibrary831应用程序集
Type t = a.GetType("ServiceReference.dll");//取得应用程序集中的NewClass类型
object o = Activator.CreateInstance(t);//创建NewClass类型的实例
//给对象的属性赋值
PropertyInfo p1 = t.GetProperty("MyName");
PropertyInfo p2 = t.GetProperty("MyInfo");
p1.SetValue(o, "中国", null);
p2.SetValue(o, "China", null);
MethodInfo mi = t.GetMethod("show");//调用对象的show方法
mi.Invoke(o, null);
}
//创建一个新的应用程序域,并加载执行程序集
//创建一个新的应用程序域,并加载执行程序集
static void Main(string[] args)
{
AppDomain currentDomain = AppDomain.CurrentDomain;
Console.WriteLine(currentDomain.FriendlyName);
AppDomain secondDomain = AppDomain.CreateDomain("New AppDomain"); //创建新的应用程序域
secondDomain.ExecuteAssembly("AssemblyA.exe");//在新的应用程序域中加载执行AssemblyA.exe程序集
}
5延迟加载:实现目录后下面显示英雄全名和技能等,正常加载时显示名称和技能后显示加载信息,现在我们想相反效果:go、。。。
创建英雄和技能类
public class Hero
{
public string FullName { get; set; }
public string Name { get; set; }
#region 引入Lazy后
private readonly Lazy<SpecialSkill> skill;//Lazy<T>按需延迟加载,属性和类的关联
public SpecialSkill Skill
{
get{ return skill.Value;}
}
public Hero(string name)
{
Name = name;
FullName = "Super " + name;
skill = new Lazy<SpecialSkill>(() => new SpecialSkill(name));
}
#endregion
}
技能类
public class SpecialSkill
{
public int Power { get; set; }
public string SkillName { get; set; }
public int StrengthSpent { get; set; }
public SpecialSkill(string name)
{
Console.WriteLine("加载特殊技能 .....");
Power = name.Length;
StrengthSpent = name.Length * 3;
SkillName = name + "筋斗云";
Console.WriteLine(SkillName + ",... 一个跟头十万八千里!");
}
}
主函数:
static void Main(string[] args)
{
////延迟加载
Hero hero = new Hero("wukong");
Console.WriteLine("\n\n..................Press Enter to continue.................\n\n");
Console.WriteLine("英雄的技能是: " + hero.Skill.SkillName);
Console.Read();
}
6////返回多个参数以及out参数使用
/// <summary>
/// 返回多个值,原函数返回空
/// </summary>
/// <param name="strOut">使用out关键字返回的字符串</param>
/// <param name="i">使用out关键字返回的数值</param>
static void ReturnMulValue(out string strOut, out int i)
{
i = 1;
strOut = "out function";
}
/// <summary>
/// 返回多个值,原函数返回数值
/// </summary>
/// <param name="strOut">使用out关键字返回的字符串</param>
/// <returns>原函数直接返回的数值</returns>
static int ReturnMulValue(out string strOut)
{
int i = 2;
strOut = "out function 2";
return i;
}
主函数:////返回多个参数以及out参数使用
static void Main(string[] args)
{
////返回多个参数以及out参数使用
int value;
string strOutValue;
ReturnMulValue(out strOutValue, out value);
Console.WriteLine("Call ReturnMulValue(out string strOut, out int i)");
Console.WriteLine("value = {0:N}", value);//value = 1.00
Console.WriteLine("Out string value = " + strOutValue); //Out string value = out function//调用函数函数原返回值与参数中均得到返回的值18
value = ReturnMulValue(out strOutValue);
Console.WriteLine("Call ReturnMulValue(out string strOut)");
Console.WriteLine("value = {0:N}", value);//value = 2.00
//Console.WriteLine("Out string value = " + strOutValue);//Out string value = out function
Console.Read();
}