使用ShowDialog窗体之间的回传值:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace _02使用ShowDialog窗体之间的回传值
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
MessageBox.Show(f2.userMsg);
MessageBox.Show("OK"); }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace _02使用ShowDialog窗体之间的回传值
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} public string userMsg; private void button1_Click(object sender, EventArgs e)
{
this.userMsg = this.textBox1.Text;
this.Close();
}
}
}

作业1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _01作业
{
class Program
{
static void Main(string[] args)
{
//Delegate1 md = M1;
//md(10,20,30); Delegate1 md = arr => Console.WriteLine(arr.Length);
md(, , );
Console.ReadKey();
} static void M1(params int[] arr)
{
Console.WriteLine(arr.Length);
}
} public delegate void Delegate1(params int[] array);
}

作业-多播委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _03多播委托
{
class Program
{
static void Main(string[] args)
{
////一个委托同时指向了多个方法,这就是多播委托
//Delegate1 md = M1;
//md += M2;
//md += M3;
//md += M4;
//md += M5;
////md = M6;//如果使用符号为委托赋值,最后会将前面的所有的绑定的委托都覆盖掉 //md -= M3;
//md -= M5; //md();//当调用委托变量的时候,绑定到该委托上的所有方法都会被调用
//Console.ReadKey(); //Delegate1 md = M1;
//md = (Delegate1)Delegate.Combine(md,new Delegate1(M2));
//md = (Delegate1)Delegate.Combine(md, new Delegate1(M3));
//md = (Delegate1)Delegate.Remove(md, new Delegate1(M1)); AddDelegate md = T1;
md += T2;
md += T3;
//多播委托如果方法有返回值的化,这里只能获取最后一个方法的返回值.
//int n=md(100,200);
//Console.WriteLine(n); //获取每个方法被调用后的返回值
Delegate[] mds = md.GetInvocationList();
//循环遍历mds,获取每个委托对象并调用
for (int i = ; i < mds.Length; i++)
{
Delegate mm = mds[i];
int n = ((AddDelegate)mm)(,);
Console.WriteLine(n);
} Console.ReadKey(); } //static int T1(int x,int y)
//{
// return x + y;
//} //static int T2(int x, int y)
//{
// return x + y;
//} //static int T3(int x, int y)
//{
// return x + y;
//} static int T1(int x, int y)
{
return ;
} static int T2(int x, int y)
{
return ;
} static int T3(int x, int y)
{
return ;
} static void M1()
{
Console.WriteLine("M1");
} static void M2()
{
Console.WriteLine("M2");
} static void M3()
{
Console.WriteLine("M3");
} static void M4()
{
Console.WriteLine("M4");
} static void M5()
{
Console.WriteLine("M5");
} static void M6()
{
Console.WriteLine("M6");
}
} public delegate int AddDelegate(int n1,int n2);
//定义一个委托
public delegate void Delegate1();
}

自定义泛型1

泛型类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04自定义泛型
{
class Program
{
static void Main(string[] args)
{
//List<int> list = new List<int>();
//泛型是代码重用和算法重用
//泛型类
//泛型接口
//泛型方法
//泛型委托 //MyClass<string> mc = new MyClass<string>(new string[] { "A", "B", "C"});
MyClass<int> mc = new MyClass<int>(new int[]{ ,,});
mc.Show();
Console.Read(); }
} //T Type
class MyClass<T>
{
public MyClass(T[] _names)
{
this.names = _names;
}
public T[] names = null; public void Show()
{
foreach (T item in names)
{
Console.WriteLine(item);
}
}
} }

泛型方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04自定义泛型
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
//p.show<string>("aaaaaaaaaaaa");
//p.show("aaaaaaaaaaaa");//这两种都是正确的 p.show<int>();
Console.ReadKey(); }
} //泛型方法
public class Person
{
//方法后直接写<T>就表示一个泛型方法
public void show<T>(T msg)
{
Console.WriteLine(msg);
}
} }

泛型接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04自定义泛型
{
class Program
{
static void Main(string[] args)
{
SpiderMan<string> sm = new SpiderMan<string>();
sm.Fly("gdsfgsdfgdsfgdsf");
Console.ReadKey(); }
} //泛型接口
public interface IFlyable<T>
{
void Fly(T msg);
} //实现泛型接口的时候必须指定对应的泛型类型
//编写一个类来实现泛型接口
//一个普通类实现了泛型接口
public class SupperMan:IFlyable<string>
{
#region MyRegion public void Fly(string msg)
{
Console.WriteLine(msg);
}
#endregion
} //一个泛型类实现了泛型接口
public class SpiderMan<YZK> : IFlyable<YZK>
{ public void Fly(YZK msg)
{
throw new NotImplementedException();
}
} //public class SpiderMan<YZK> : IFlyable<string>
//{ // public void Fly(YZK msg)
// {
// throw new NotImplementedException();
// }
//}
}

泛型委托:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04自定义泛型
{
public delegate void M1Delegate(string msg);
public delegate void MGenericDelegate<T>(T msg);
class Program
{
static void Main(string[] args)
{
#region 泛型委托 //M1Delegate md = M1;
//md("aaaaaaaaaaaaa");
//Console.ReadKey(); //MGenericDelegate<string> md = M1;
//md("aaaaaaaaaaaaa");
//Console.ReadKey(); //MGenericDelegate<int> md = M2;
//md(123);
//Console.ReadKey(); MGenericDelegate<double> md = M3;
md(123.45);
Console.ReadKey(); #endregion } static void M1(string msg)
{
Console.WriteLine(msg);
}
static void M2(int msg)
{
Console.WriteLine(msg);
}
static void M3(double msg)
{
Console.WriteLine(msg);
}
static void M4(float msg)
{
Console.WriteLine(msg);
}
static void M5(Person msg)
{
Console.WriteLine(msg);
}
} class Person
{ } }

泛型委托:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04自定义泛型
{
public delegate void M1Delegate(string msg);
public delegate void MGenericDelegate<T>(T msg);
class Program
{
static void Main(string[] args)
{
#region 系统内置的泛型委托
//只要是Action委托都是无返回值的 ////存储无参数无返回值的方法
//Action md = () => { Console.WriteLine("无参数无返回值。"); };
//md();
//Console.ReadKey(); ////存储有几个参数的返回值
//Action<string,int> md = (s,i) => { Console.WriteLine(s+" "+i); };
//md("aaaaaaaaaaaa",123);
//Console.ReadKey(); //带返回值的方法的时候,就需要使用另外一个泛型委托Func //Func<string> fn = T1;
//string ss = fn();
//Console.WriteLine(ss);
//Console.ReadKey(); ////返回值是string类型,参数是一个int类型
//Func<int, string> fn = n => n.ToString();
//Console.WriteLine(fn(10));
//Console.ReadKey(); ////返回值是string类型,参数是一个int类型
//Func<int,int, string> fn =T2;
//Console.WriteLine(fn(12,5));
//Console.ReadKey(); List<int> list = new List<int>{,,,,,,,,};
//List<int> listresult = list.FindAll(MyFilter);
List<int> listresult = list.FindAll(element=>element>);
//list.Where(a => a > 5);
for (int i = ; i < listresult.Count; i++)
{
Console.WriteLine(listresult[i]);
}
Console.ReadKey(); #endregion }
static bool MyFilter(int element)
{
return element>;
}
static string T1()
{
return "aaaaaaaaaaaaa";
} static string T2(int n1,int n2)
{
return (n1*n2).ToString();
}
} }

泛型约束:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _05泛型约束
{
class Program
{
static void Main(string[] args)
{
//List<int>list=new List<int>();
//list.Sort(); //List<Person> list = new List<Person>();
//list.Sort(); //MyClass<string> mc = new MyClass<string>();
//MyClass<int> mc = new MyClass<int>(); //MyClass<MyTestClass> mc = new MyClass<MyTestClass>(); MyClass<Person> mc = new MyClass<Person>(); }
} class MyTestClass : IComparable
{ public int CompareTo(object obj)
{
throw new NotImplementedException();
}
}
//class AscByAge : IComparable<Person>
//{ // public int CompareTo(Person x,Person y)
// {
// throw new NotImplementedException();
// }
//} class Person:IComparable<Person>
{
//public Person(string s)
//{ //}
private int _age; public int Age
{
get { return _age; }
set { _age = value; }
} private string _name; public string Name
{
get { return _name; }
set { _name = value; }
} public int CompareTo(Person other)
{
throw new NotImplementedException();
}
} ////使用泛型约束,约束了T只能是值类型
//class MyClass<T> where T:struct
//{ //} ////使用泛型约束,约束了T只能是引用类型不能是值类型
//class MyClass<T> where T : class
//{ //} //限制T必须是实现了某个接口的类型,要求T必须是实现了IComparable接口的子类型对象或者就是该接口类型对象
//class MyClass<T> where T : IComparable
//{ //} ////要求T必须是Person类型或者是Person类的子类型
//class MyClass<T> where T : Person
//{ //} ////要求T必须是Person类型或者是Person类的子类型
//class MyClass<T>
// where T : Person
// where T : new()//要求将来传递进来的类型必须具有一个无参数的构造函数
//{ //} //对T没有要求,但是V必须是T类型或者T类型的子类型
class MyClass<T,V>
where V :T
{ }
}

扩展方法:

先建一个静态类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _07扩展方法
{
/// <summary>
/// 1.增加扩展方法第一步,增加一个静态类,类名随便起
/// 该静态类应该与将来要用扩展方法的地方在一个命名空间下。
/// 即便命名空间不一样,用的时候也必须导入该命名空间否则不能使用
/// </summary>
static class MethodExt
{
//2.向静态类中增加一个静态方法
//该静态方法的第一个参数就表示要给哪个类型增加该扩展方法
//第一个修饰符this是必须的,这里的obj就表示将来调用该方法的那个
public static void SayHello(this Person obj)
{
Console.WriteLine("Helllo"+obj.Name);
}
}
}

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _07扩展方法
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "叶长种";
p.SayHi(); //扩展方法
p.SayHello();
Console.ReadKey();
}
} class Person
{
public string Name{get;set;}
public void SayHi()
{
Console.WriteLine("Hi~~~~~~~~~~~~");
}
}
}

事件介绍:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _08事件介绍
{
class Program
{
static void Main(string[] args)
{
Mp3Palyer mp3 = new Mp3Palyer();
mp3.BeforeMusicPlaying = () => { Console.WriteLine("音乐播放之前加载歌曲........."); };
mp3.BeforeMusicStoping = () => { Console.WriteLine("音乐结束之前保存播放进度........."); };
mp3.Start();
mp3.PowerOff();
Console.ReadKey();
}
} /// <summary>
/// Mp3播放器
/// </summary>
public class Mp3Palyer
{
//希望在音乐开始播放之前插入一段代码,这段代码是由将来调用者来指定的
public Action BeforeMusicPlaying; //希望在音乐停止播放之前插入一段代码,这段代码也是由将来调用者来指定的
public Action BeforeMusicStoping; //启动Mp3
public void Start()
{
Console.WriteLine("启动");
if (BeforeMusicPlaying!=null)
{
//在音乐正式播放之前调用该委托
BeforeMusicPlaying();
}
//调用 PlayMusic()方法开始播放音乐
PlayMusic();
} private void PlayMusic()
{
Console.WriteLine("音乐开始播放。。。。。。。。。。");
} //关机
public void PowerOff()
{
if (BeforeMusicStoping != null)
{
//在音乐结束之前调用该委托
BeforeMusicStoping();
}
//调用停止播放音乐的方法
Stop();
} //启动Mp3
public void Stop()
{
Console.WriteLine("音乐停止播放");
}
}
}

事件案例:

创建控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace _09事件案例
{
public partial class UCTripleButton : UserControl
{
public UCTripleButton()
{
InitializeComponent();
} //当在声明委托变量的前面加了event关键字自后,则委托变量就不是委托变量了,就立刻变成了事件
public event Action TripleClick;
int count = ;
//这个是按钮的单击事件
private void button_Click(object sender, EventArgs e)
{
//每次单击一次统计一下
count++;
if (count>=)
{
//MessageBox.Show("点了3次!");
if (TripleClick!=null)
{
//调用一下委托
TripleClick();
} count = ;
}
}
}
}

窗体1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace _09事件案例
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //这里是按钮的单击事件
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("你好!!!!!");
} //委托可以直接赋值,但是事件不能直接赋值
//事件只能使用+=或者-=来赋值,这样就避免了通过=将之前的注册的事件都覆盖掉
private void Form1_Load(object sender, EventArgs e)
{
//ucTripleButton1.TripleClick = ClickThree;
ucTripleButton1.TripleClick += new Action(ucTripleButton1_TripleClick);
} void ucTripleButton1_TripleClick()
{
Console.WriteLine("事件处理");
}
private void ClickThree()
{
MessageBox.Show("被点击了三次!");
}
}
}

窗体2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace _09事件案例
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
//ucTripleButton1.TripleClick = Click3;
ucTripleButton1.TripleClick += new Action(ucTripleButton1_TripleClick);
} void ucTripleButton1_TripleClick()
{
Console.WriteLine("事件处理");
}
public void Click3()
{
MessageBox.Show("哇塞!!!!被点击了三次哦!!!");
} }
}

12---Net基础加强的更多相关文章

  1. 【转载】Python编程中常用的12种基础知识总结

    Python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进 ...

  2. Python编程中常用的12种基础知识总结

    原地址:http://blog.jobbole.com/48541/ Python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时 ...

  3. [nRF51822] 12、基础实验代码解析大全 · 实验19 - PWM

    一.PWM概述: PWM(Pulse Width Modulation):脉冲宽度调制技术,通过对一系列脉冲的宽度进行调制,来等效地获得所需要波形. PWM 的几个基本概念: 1) 占空比:占空比是指 ...

  4. 12.Django基础十之Form和ModelForm组件

    一 Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户 ...

  5. python学习之旅1-2(基础知识)

    三,python基础初识. 1.运行python代码. 在d盘下创建一个t1.py文件内容是: print('hello world') 打开windows命令行输入cmd,确定后 写入代码pytho ...

  6. 利用Python进行数据分析(12) pandas基础: 数据合并

    pandas 提供了三种主要方法可以对数据进行合并: pandas.merge()方法:数据库风格的合并: pandas.concat()方法:轴向连接,即沿着一条轴将多个对象堆叠到一起: 实例方法c ...

  7. python基础-第十二篇-12.1jQuery基础与实例

    一.查找元素 1.选择器 基本选择器 $("*") $("#id") $(".class") $("element") ...

  8. 12. Mysql基础入门

    课程大纲 • 数据库概述 • MySQL基本操作 • MySQL索引基础 • MySQL高级特性

  9. 12.web基础与HTTP协议

    web基础与HTTP协议 目录 web基础与HTTP协议 web基础 域名概述 HTML概述 HTML基本标签 HTML语法规则 HTML文件结构 头标签中常用标签 内容标签中常用标签 静态网页与动态 ...

  10. (转)iOS7界面设计规范(12) - UI基础 - 品牌

    重要:这是针对于正在开发中的API或技术的预备文档(预发布版本).虽然该文档在技术精确度上经过了严格的审核,但并非最终版本,仅供苹果开发者计划的注册会员使用.苹果提供这份机要文档的目的,是帮助你按照文 ...

随机推荐

  1. HBase的属性

    一:基本属性 1.查看属性 2.解释属性 NAME:列簇名 BLOOMFILTER:布隆过滤器,用于对storefile的过滤 共有三种类型: ROW:行健过滤 ROWCOL:行列过滤 NONE:无 ...

  2. php--yii2.0的安装

    1.php.ini中去掉php_openssl.dll前面的“;” 2.注意phpstudy中php版本使用5.4n 3.环境OK后,使用自己的域名访问下yii2.0中advanced中的requir ...

  3. ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100)

    原文:ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100) 对于非地理专业的开发人员,对与这些生涩的概念,我们不一定都要了解,但是我们要理解,凡是 ...

  4. 如何解决jQuery Validation针对动态添加的表单无法工作的问题?

    为了充分利用ASP.NET MVC在服务端呈现HTML的能力,在<利用动态注入HTML的方式来设计复杂页面>一文中介绍了,通过Ajax调用获取HTML来呈现复杂页面中某一部分界面的解决方案 ...

  5. [转]AppCompat 22.1,Goole暴走,MD全面兼容低版本

    AppCompat 22.1,Goole暴走,MD全面兼容低版本 分类: Android2015-04-24 09:48 1354人阅读 评论(0) 收藏 举报 android   目录(?)[+] ...

  6. SQLServer Note

    1. Grant necessory permission to user account, so it can use SQL profiler. USE masterGRANT ALTER TRA ...

  7. OpenGL学习笔记:拾取与选择

    转自:OpenGL学习笔记:拾取与选择 在开发OpenGL程序时,一个重要的问题就是互动,假设一个场景里面有很多元素,当用鼠标点击不同元素时,期待作出不同的反应,那么在OpenGL里面,是怎么知道我当 ...

  8. nohup和&的区别

    nohup和&的区别http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4241330&fromuid=21288388 ...

  9. iOS小技巧2

    这段代码是实现了类似QQ空间"我的空间"里面的圆形头像 //圆形的头像 UIImageView * headImage = [[UIImageView alloc]initWith ...

  10. 右键TXT文件-打开方式-选择默认程序提示 windows无法访问指定设备,路经或文件.您可能没有合适的权限访问这个项目

    新建立一个.txt文本,把下面复制到.txt文本点保存,再把.txt文件格式修改为.reg格式, 双击导入注册表就OK了 Windows Registry Editor Version 5.00 [H ...