使用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. 帝国CMS备忘

    一. 2级导航: 类似下图这种导航: 实现方式如: 1. 定义一个标签模板,记住ID,具体内容如: 页面模板内容: <li><a href=”[!—bclassurl—]”>[ ...

  2. [LeetCode]题解(python):047-Permutations II

    题目来源 https://leetcode.com/problems/permutations-ii/ Given a collection of numbers that might contain ...

  3. (转载)C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind

    http://blog.sina.com.cn/s/blog_61437b3b0102v0bt.html http://blog.csdn.net/chenwk891/article/details/ ...

  4. xdebug和xhprof

    在安装时出现不是:1% 不是有效的win32 应用程序原因可能是是下载了64位的.dll扩展与当前的php不兼容

  5. imx6 启动 init进程

    之前不知道imx6内核是怎么启动文件系统的init进程,查了下资料,记录于此,以后再来补充. kernel/init/main.c static noinline int init_post(void ...

  6. JavaScript:日期选择器组件的使用

    前言: 在实际项目开发中,日期选择是一个十分常见而且重要的问题,在表单中设计到日期的验证时,如果让用户自己输入时间的话,那么使用正则进行验证其正确性是不可取的,因为他一般只能验证日期的格式,无法准确的 ...

  7. 十大技巧快速提升原生APP开发性能

    移动应用市场用户争夺战日益激烈,原来做APP拼想法拼创意拼是否抓住用户痛点.现在,精细化用户体验成为了一个APP能否留存用户的关键问题,一旦用户觉得体验不畅,马上就有竞品APP后补,如何开发高性能的移 ...

  8. java进阶书籍推荐

    第一部分: Java语言篇 1 <Java编程规范>   星级:   适合对象:初级,中级   介绍:作者James Gosling(Java之父),所以这本书我觉得你怎么也得读一下.对基 ...

  9. ava获得当前文件路径

    第一种: File f = new File(this.getClass().getResource("/").getPath()); System.out.println(f); ...

  10. sqlite数据库 adb 从配置到查询表中数据全过程-----献给初学的自己

    1.   E:\Android\android-sdk-windows\platform-tools[将adb.exe文件的路径放到path中,设置环境变量] 2.  adb -s emulator ...