委托:
static void Main(string[] args){
直接调用方法
//Console.WriteLine(Class2.Say("张三"));
使用委托调用
创建一个委托的对象,传入参数为方法Say
//Class2.DelSay delt = new Class2.DelSay(Class2.Say);
//Console.WriteLine(delt("李四"));
也可以这么写,直接把方法Say装入委托对象
使用委托指向的方法代替委托对象
//Class2.DelSay delt2 = Class2.Say;
//Console.WriteLine(delt2("王五"));
或者直接调用Test
这里传入的参数是一个委托,使用他指向的方法来作为参数传入
//string result = Class2.GetSay("赵六", Class2.Say);
//Console.WriteLine(result);
//Console.ReadKey();
string str = "aBdfReg";
Console.WriteLine(Class1.ProStr(str, Class1.StrToLower));
Console.WriteLine(Class1.ProStr(str, Class1.StrToupper));
Class1.DelStr del1 = new Class1.DelStr(Class1.StrToupper);
Console.WriteLine(del1(str));
Class1.DelStr del2 = new Class1.DelStr(Class1.StrToLower);
Console.WriteLine(del2(str));
Console.ReadKey();
}
Class2代码
public class Class2
{
/// <summary>
/// 定义一个委托
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public delegate string DelSay(string name);
/// <summary>
/// 创建一个
/// </summary>
/// <param name="name"></param>
/// <param name="del"></param>
/// <returns></returns>
public static string GetSay(string name, DelSay del)
{
return del(name);
}
/// <summary>
/// 创建一个委托指向的方法
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string Say(string name)
{
return name + "说盈盈是个大美女";
}
}
Class1代码
public class Class1
{
/// <summary>
/// 定义委托
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public delegate string DelStr(string str);
/// <summary>
/// 调用转换的方法
/// </summary>
/// <param name="str"></param>
/// <param name="del"></param>
/// <returns></returns>
public static string ProStr(string str, DelStr del)
{
string result = null;
char[] buffer = str.ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
result += del(buffer[i].ToString());
}
return result;
}
/// <summary>
/// 转换成大写
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StrToupper(string str)
{
return str.ToUpper();
}
/// <summary>
/// 转换成小写
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string StrToLower(string str)
{
return str.ToLower();
}
}
匿名函数
/// <summary>
/// 匿名函数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public delegate string DelNMHS(string str);
static void Main(string[] args)
{
匿名函数
//DelNMHS del = delegate(string str)
//{
// return str + "是小猪佩奇";
//};
//Console.WriteLine(del("张三"));
//Console.ReadKey();
//或者使用lamda表达式书写
DelNMHS del2 = (string str) => { return str + "回家吃奶去"; };
Console.WriteLine(del2("李四"));
Console.ReadKey();
}
xml操作:
static void Main(string[] args)
{
CreateXml();
CreateXmlItem();
}
/// <summary>
/// 创建一个xml文档
/// </summary>
public static void CreateXml()
{
//创建一个xml对象
XmlDocument xdoc = new XmlDocument();
//创建xml文档版本号和编码类型
XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
//把版本和编码类型添加到xdoc中
xdoc.AppendChild(dec);
//创建根节点Books
XmlElement books = xdoc.CreateElement("Books");
xdoc.AppendChild(books);
//为Books创建根节点的第一个子节点及其内容
XmlElement book1 = xdoc.CreateElement("Book");
books.AppendChild(book1);
//为book1添加子节点及内容
XmlElement name1 = xdoc.CreateElement("Name");
name1.InnerXml = "战国策";
book1.AppendChild(name1);
XmlElement price1 = xdoc.CreateElement("Price");
price1.InnerXml = "100";
book1.AppendChild(price1);
XmlElement des1 = xdoc.CreateElement("Des");
des1.InnerXml = "历史书啊思密达";
book1.AppendChild(des1);
//为Books创建跟节点的第二个子节点及其内容
XmlElement book2 = xdoc.CreateElement("Book");
books.AppendChild(book2);
//为book1添加子节点及内容
XmlElement name2 = xdoc.CreateElement("Name");
name2.InnerXml = "水浒传";
book2.AppendChild(name2);
XmlElement price2 = xdoc.CreateElement("Price");
price2.InnerXml = "13";
book2.AppendChild(price2);
XmlElement des2 = xdoc.CreateElement("Des");
des2.InnerXml = "喜欢武松";
book2.AppendChild(des2);
//保存
xdoc.Save(@"C:\Users\Administrator\Desktop\test\1.xml");
}
/// <summary>
/// 创建一个带属性的xml文件
/// </summary>
public static void CreateXmlItem()
{
//创建一个xml对象
XmlDocument xdoc = new XmlDocument();
//创建xml文档版本号和编码类型
XmlDeclaration dec = xdoc.CreateXmlDeclaration("1.0", "utf-8", null);
//把版本和编码类型添加到xdoc中
xdoc.AppendChild(dec);
//创建根节点
XmlElement orders = xdoc.CreateElement("Orders");
xdoc.AppendChild(orders);
//创建根节点的第一个子节点及其内容
XmlElement order1 = xdoc.CreateElement("Order");
orders.AppendChild(order1);
//为order1添加子节点及内容
XmlElement name1 = xdoc.CreateElement("Name");
name1.InnerXml = "丽丽";
order1.AppendChild(name1);
XmlElement number1 = xdoc.CreateElement("Number");
number1.InnerXml = "BJ333338";
order1.AppendChild(number1);
//添加带属性的节点
XmlElement items1 = xdoc.CreateElement("Items");
order1.AppendChild(items1);
XmlElement item11 = xdoc.CreateElement("Item");
item11.SetAttribute("Name", "香水");
item11.SetAttribute("Count", "3");
item11.SetAttribute("Price", "120");
item11.SetAttribute("TotPrice", (3 * 120).ToString ());
items1.AppendChild(item11);
XmlElement item12 = xdoc.CreateElement("Item");
item12.SetAttribute("Name", "口红");
item12.SetAttribute("Count", "30");
item12.SetAttribute("Price", "240");
item12.SetAttribute("TotPrice", (30 * 240).ToString());
items1.AppendChild(item12);
//创建跟节点的第二个子节点及其内容
XmlElement order2 = xdoc.CreateElement("Order");
orders.AppendChild(order2);
//为order1添加子节点及内容
XmlElement name2 = xdoc.CreateElement("Name");
name2.InnerXml = "硕硕";
order2.AppendChild(name2);
XmlElement number2 = xdoc.CreateElement("Number");
number2.InnerXml = "BJ333339";
order2.AppendChild(number2);
//添加带属性的节点
XmlElement items2 = xdoc.CreateElement("Items");
order2.AppendChild(items2);
XmlElement item21 = xdoc.CreateElement("Item");
item21.SetAttribute("Name", "香水");
item21.SetAttribute("Count", "3");
item21.SetAttribute("Price", "120");
item21.SetAttribute("TotPrice", (3 * 120).ToString());
items2.AppendChild(item21);
XmlElement item22 = xdoc.CreateElement("Item");
item22.SetAttribute("Name", "口红");
item22.SetAttribute("Count", "30");
item22.SetAttribute("Price", "240");
item22.SetAttribute("TotPrice", (30 * 240).ToString());
items2.AppendChild(item22);
//保存
xdoc.Save(@"C:\Users\Administrator\Desktop\test\2.xml");
}