单例模式:
只能创建一个对象:
public partial class Form2 : Form
{
//全局唯一的单例
public static Form2 FrmSingle=null;
private Form2()
{
InitializeComponent();
}
public static Form2 GetSingle()
{
if (FrmSingle == null)
{
FrmSingle = new Form2();
}
return FrmSingle;
}
}
XML:
可扩展标记语言,xml存储数据
xml严格区分大小写
创建xml文档
static void Main(string[] args)
{
//通过代码来创建XML文档
//1、引用命名空间
//2、创建XML文档对象
XmlDocument doc = new XmlDocument();
//3、创建第一个行描述信息,并且添加到doc文档中
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
//4、创建根节点
XmlElement books = doc.CreateElement("Books");
//将根节点添加到文档中
doc.AppendChild(books);
//5、给根节点Books创建子节点
XmlElement book1 = doc.CreateElement("Book");
//将book添加到根节点
books.AppendChild(book1);
//6、给Book1添加子节点
XmlElement name1 = doc.CreateElement("Name");
name1.InnerText = "中国";
book1.AppendChild(name1);
XmlElement price1 = doc.CreateElement("Price");
price1.InnerText = "10";
book1.AppendChild(price1);
XmlElement des1 = doc.CreateElement("Des");
des1.InnerText = "好看";
book1.AppendChild(des1);
XmlElement book2 = doc.CreateElement("Book");
books.AppendChild(book2);
XmlElement name2 = doc.CreateElement("Name");
name2.InnerText = "中国";
book2.AppendChild(name2);
XmlElement price2= doc.CreateElement("Price");
price2.InnerText = "10";
book2.AppendChild(price2);
XmlElement des2 = doc.CreateElement("Des");
des2.InnerText = "好看";
book2.AppendChild(des2);
doc.Save("Books.xml");
//成功后的路径在bin--Debug里
Console.WriteLine("保存成功");
Console.ReadKey();
}
创建带属性的XML文档
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes");
doc.AppendChild(dec);
XmlElement order = doc.CreateElement("Order");
doc.AppendChild(order);
XmlElement customerName = doc.CreateElement("CustomerName");
customerName.InnerXml = "<p>我是一个p标签</p>";
order.AppendChild(customerName);
XmlElement customerNumber = doc.CreateElement("CustomerNumber");
customerNumber.InnerText = "<p>我是一个p标签</p>";
order.AppendChild(customerNumber);
XmlElement items = doc.CreateElement("Items");
order.AppendChild(items);
XmlElement orderItem1 = doc.CreateElement("OrderItem");
//给节点添加属性
orderItem1.SetAttribute("Name", "气");
orderItem1.SetAttribute("Count", "10");
items.AppendChild(orderItem1);
XmlElement orderItem2 = doc.CreateElement("OrderItem");
//给节点添加属性
orderItem2.SetAttribute("Name", "气");
orderItem2.SetAttribute("Count", "10");
items.AppendChild(orderItem2);
XmlElement orderItem3 = doc.CreateElement("OrderItem");
//给节点添加属性
orderItem3.SetAttribute("Name", "气球");
orderItem3.SetAttribute("Count", "10");
items.AppendChild(orderItem3);
doc.Save("Order.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
}
追加xml
//追加XML文档
XmlDocument doc = new XmlDocument();
XmlElement books;
if (File.Exists("Books.xml"))
{
//如果文件存在 加载XML
doc.Load("Books.xml");
//获得文件的根节点
books = doc.DocumentElement;
}
else
{
//如果文件不存在
//创建第一行
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
//创建跟节点
books = doc.CreateElement("Books");
doc.AppendChild(books);
}
//5、给根节点Books创建子节点
XmlElement book1 = doc.CreateElement("Book");
//将book添加到根节点
books.AppendChild(book1);
//6、给Book1添加子节点
XmlElement name1 = doc.CreateElement("Name");
name1.InnerText = "c#开发大全";
book1.AppendChild(name1);
XmlElement price1 = doc.CreateElement("Price");
price1.InnerText = "110";
book1.AppendChild(price1);
XmlElement des1 = doc.CreateElement("Des");
des1.InnerText = "看不懂";
book1.AppendChild(des1);
doc.Save("Books.xml");
Console.WriteLine("保存成功");
Console.ReadKey();
读取带属性的XML
//读取带属性的XML文档
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem");//路径是xml里面的标签
foreach (XmlNode node in xnl)
{
Console.WriteLine(node.Attributes["Name"].Value);
Console.WriteLine(node.Attributes["Count"].Value);
}
Console.ReadKey();
委托
将一个方法作为参数传递给另一个方法,方法的类型是委托类型
委托所指向的函数必须和委托具有相同的签名
签名就是参数和返回值
委托的理解:
//声明一个委托指向一个函数,返回值和参数一致
public delegate void DelSayHi(string name);
class Program
{
static void Main(string[] args)
{
//第一种写法
/*DelSayHi del =new DelSayHi(SayHiEnglish);
del("张三");
Console.ReadKey();*/
//第二种写法
/*DelSayHi del = SayHiEnglish;
del("张三");
Console.ReadKey();*/
//第三种写法
Test("张三", SayHiChinese);
Test("李四", SayHiEnglish);
Console.ReadKey();
}
public static void Test(string name,DelSayHi del)
{
//调用
del(name);
}
public static void SayHiChinese(string name)
{
Console.WriteLine("吃了么?" + name);
}
public static void SayHiEnglish(string name)
{
Console.WriteLine("Nice to meet you" + name);
}
匿名函数
DelSayHi del = delegate(string name)
{
Console.WriteLine("你好" + name);
};
del("张三");
}
//public static void SayHi(string name,DelSayHi del)
//{
// del(name);
//}
//public static void SayHiChinese(string name)
//{
// Console.WriteLine("你好"+name);
//}
//public static void SayHiEnglish(string name)
//{
// Console.WriteLine("Hello"+name);
//}
}
lamda表达式(匿名函数的简写)
//lamda表达式 => goes to
DelSayHi del = (string name) => { Console.WriteLine("你好" + name); };
del("张三");
Console.ReadKey();
}
//public static void SayHi(string name,DelSayHi del)
//{
// del(name);
//}
//public static void SayHiChinese(string name)
//{
// Console.WriteLine("你好"+name);
//}
//public static void SayHiEnglish(string name)
//{
// Console.WriteLine("Hello"+name);
//}
匿名函数和lamda表达式对比
static void Main(string[] args)
{
DelOne del = () => { };// delegate() { };
DelTwo del2 = (string name) => { };//delegate(string name) { };
DelThree del3 = (string name) => { return name; };//delegate(string name) { return name; };
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.RemoveAll(n => n > 4);//括号内的也是lamda表达式
foreach (var item in list)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
泛型委托
object需要装箱拆箱,用T替代
类型用T代替,T就是type类型的缩写
窗体传值
需要使用委托进行传值
把Form1的方法传递给Form2
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(ShowMsg);
frm2.Show();
}
void ShowMsg(string str)
{
label1.Text = str;
}
}
Form2
//声明一个委托
public delegate void DelTest(string str);
public partial class Form2 : Form
{
public DelTest _del;
public Form2(DelTest del)
{
this._del = del;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_del(textBox1.Text);
}
}
多播委托
委托可以指向多个函数,执行时同时执行
+=就是再绑定一个函数给委托
-=就是把绑定好的函数给删除
public delegate void DelTest();
static void Main(string[] args)
{
DelTest del = T1;
del += T2;
del += T3;
del+= T4;
del -= T3;
del -= T1;
del();
Console.ReadKey();
}
public static void T1()
{
Console.WriteLine("我是T1");
}
public static void T2()
{
Console.WriteLine("我是T2");
}
public static void T3()
{
Console.WriteLine("我是T3");
}
public static void T4()
{
Console.WriteLine("我是T4");
}