C#:iterator 迭代器/partial class 分布类/泛型

news/2024/6/18 19:20:34

iterator 迭代器

写个最简单的迭代,(迭代一个字符串数组):

1.实现接口中的方法:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Collections;   //动态数组是在这个命名空间下的,所以使用前要包含
 7 
 8 namespace ConsoleApplication1
 9 {
10     //interator_class是继承IEnumerable接口的类
11     class interator_class:System.Collections.IEnumerable 
12     {
13         //写一个要迭代的字符串数组
14         ArrayList al = new ArrayList() {"大家好!","我是李晓峰","希望大家多多指教!","谢谢" };
15 
16         //实现GETEnumerator()该方法
17         public System.Collections.IEnumerator GetEnumerator()    
18         {
19             for (int i = 0; i < 4; i++)
20             { yield return al[i]; }       //返回
21         }
22     }
23 }

2.调用:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //实例化interator_class类
14             interator_class ic = new interator_class();  
15 
16             //迭代
17             foreach (string al_ary in ic)
18             {
19                 Console.WriteLine(al_ary+"\n");
20             }
21 
22             //聚焦显示
23             Console.ReadLine();
24         }
25     }
26 }

3.运行:

 

partial class 分布类:

      简单说就是将一个类在一个命名空间内解剖开:

例如:写一个partial类,每个class中分布一个方法():

1.分布类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace WindowsFormsApplication1
 8 {
 9    //写4个分布类,每个类中分别写了一个运算方法:
10 
11    public partial  class jisuan_fangfa
12     {
13        public int jiafa(int a,int b)
14        { return a + b; }
15     }
16 
17    public partial class jisuan_fangfa
18    {
19        public int jianfa(int a, int b)
20        { return a - b; }
21    }
22 
23    public partial class jisuan_fangfa
24    {
25        public int chengfa(int a, int b)
26        { return a * b; }
27    }
28 
29 
30    public partial class jisuan_fangfa
31    {
32        public int chufa(int a, int b)
33        { return a /b; }
34    }
35 }

2.调用:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace WindowsFormsApplication1
12 {
13     public partial class Form6 : Form
14     {
15         public Form6()
16         {
17             InitializeComponent();
18         }
19 
20         private void Form6_Load(object sender, EventArgs e)
21         {
22             //默认回车键为 计算;
23             AcceptButton = button1;
24 
25             //向combobox内添加选项;
26             comboBox1.Items.Add(""); comboBox1.Items.Add("");
27             comboBox1.Items.Add(""); comboBox1.Items.Add("");
28 
29             //combobox默认选择 加法
30             comboBox1.SelectedIndex = 0;
31 
32             this.toolStripStatusLabel1.Text = "欢迎使用!";
33 
34         }
35 
36         private void button1_Click(object sender, EventArgs e)
37         {
38             if (textBox1.Text == string.Empty || textBox2.Text == string.Empty)
39             {
40                 MessageBox.Show("请输入要运算的数据!");
41 
42                 //提示输入信息:
43                 if (textBox1.Text == string.Empty)
44                 { errorProvider1.SetError(textBox1, "这里不能为空!"); }
45                 if (textBox2.Text == string.Empty)
46                 { errorProvider1.SetError(textBox2, "这里不能为空!"); }
47             }
48             else
49             {
50                 //实例化类
51                 jisuan_fangfa jisuan = new jisuan_fangfa();
52 
53                 switch (comboBox1.Text.Trim())   //判断combobox中选择计算的模式
54                 {
55                         //数据的转换/判断
56                     case "": textBox3.Text=jisuan.jiafa(Convert.ToInt32(textBox1.Text.Trim()),Convert.ToInt32(textBox2.Text.Trim())).ToString(); break;
57                     case "": textBox3.Text = jisuan.jianfa(Convert.ToInt32(textBox1.Text.Trim()), Convert.ToInt32(textBox2.Text.Trim())).ToString(); break;
58                     case "": textBox3.Text = jisuan.chengfa(Convert.ToInt32(textBox1.Text.Trim()), Convert.ToInt32(textBox2.Text.Trim())).ToString(); break;
59                     case "": textBox3.Text = jisuan.chufa(Convert.ToInt32(textBox1.Text.Trim()), Convert.ToInt32(textBox2.Text.Trim())).ToString(); break;
60                 }
61 
62             }
63         }
64     }
65 }

3.实践:

    

 

泛型:

       泛型是具有占位符(类型参数)的类、结构、接口和方法,这些占位符是类、结构、接口和方法所存储或使用的一个或多个类型的占位符。

1.接口:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9 public interface Interface1<T>
10     {
11     T fangfa();
12     }
13 }

2.继承泛型接口的类:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9      //Class_t类中有2个泛型,T是继承于T1,T可以实例化
10    public  class Class_t<T,T1>:Interface1 <T1 > where T:T1, new()
11     {
12        //实现该方法:
13        public T1 fangfa()
14        { return new T(); }
15       
16     }
17 }

3.实例化,调用:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //实例化,并添加类型
14             Interface1<System.ComponentModel.IListSource> inter = new Class_t<System.Data.DataTable, System.ComponentModel.IListSource>();
15            
16             //输出inter.fangfa()的类型
17             Console.WriteLine("T是:"+inter.fangfa().GetType().ToString()+"类型");  
18             Console.ReadLine();
19         }
20     }
21 }

4.运行:

 

2.方法();

   写一个用户名和密码的验证方法:

1.方法:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9   public   class Class1
10     {
11       public void error_show<T, T1>(T yonghuming, T1 mima)
12       {
13           if (string.Equals(yonghuming, "lixiaofeng") && string.Equals(mima.ToString(), "123"))
14           {
15               Console.WriteLine("验证成功!");
16           }
17           else
18           { Console.WriteLine("验证失败,你的输入信息有问题!"); }
19 
20           
21       }
22   
23     }
24 }

2.调用:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication2
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int mii;  //定义密码变量
14 
15             //接受用户输入的用户名和密码
16             Console.WriteLine("请输入用户名:"); string mizi = Console.ReadLine();
17             Console.WriteLine("请输入密码:"); mii = Convert.ToInt32(Console.ReadLine());
18             
19             //实例化class1,并填入泛型类型
20             Class1 c1 = new Class1();
21             c1.error_show<string, int>(mizi, mii );
22 
23             Console.ReadLine();
24         }
25     }
26 }

3.验证:

 

水平有限,还望指教,谢谢!

 

转载于:https://www.cnblogs.com/liyifeng/p/C%e5%a4%8f%e6%99%ae.html


http://www.niftyadmin.cn/n/2556775.html

相关文章

学习:SQL数据库日志收缩(转)

declare data varchar(20) select data 数据库名 execute (execute sp_helpdb data ) execute ( BACKUP LOG data WITH TRUNCATE_ONLY ) execute ( DBCC SHRINKDATABASE( data ,10)) execute (execute sp_helpdb data ) 文章来源&#xff1a;http://bbs.winos.cn/v…

基本知识点罗列

1.dropdownlist的绑定 BLLAuction bll_getAuctionnew BLLAuction();this.ddlauctioncode.DataSource bll_getAuction.GetAuctionList();this.ddlauctioncode.DataTextField "Name";this.ddlauctioncode.DataValueField "code";this.ddlauctioncode.Data…

Administrator用户直接获取SYSTEM权限

来源&#xff1a; http://www.nsfocus.com 作者&#xff1a;"scz" < scznsfocus.com> 标题: MSDN系列(3)--Administrator用户直接获取SYSTEM权限 日期: 2003-06-21 21:51更新: --------------------------------------------------------------------------…

web developer tips (26):在 App_Code目录下同时放c#和VB.NET文件

原文地址&#xff1a;How to have C# and VB.NET files inside your App_Code directory 如果你利用App_Code目录来开发一个Asp.net web网站&#xff0c;有时候需要写用不同net语言的代码文件。例如&#xff0c;如果你想用在同一个web网站同时使用c#和VB.net http://www.watch-…

IE盒子模型和标准框元素模型

1、ie模型 2、w3c模型 3、box-sizing 4、*{box-sizing:border-box; -moz-box-sizing:border-box; -webkit-box-sizing:border-box;} 记录一个重要的属性的box-sizing TODO&#xff1a;转载于:https://www.cnblogs.com/linksgo2011/p/3268624.html

火狐插件Firebug的使用

什么是Firebug 从事了数年的Web开发工作&#xff0c;越来越觉得现在对WEB开发有了更高的要求。要写出漂亮的HTML代码&#xff1b;要编写精致的CSS样式表展示每个页面模块&#xff1b;要调试javascript给页面增加一些更活泼的要素&#xff1b;要使用Ajax给用户带来更好的体验。一…

each与list的用法(PHP学习)

1.each的用法 先看API array each ( array &$array )api里是这么描述的&#xff1a;each — 返回数组中当前的键&#xff0f;值对并将数组指针向前移动一步 我们先来看看返回的数组是怎么样的&#xff1f; <?php $arr array(你,若,安,好,便,是,晴,天); print_r(each($…

Web前端性能优化全攻略

Yahoo! 的 Exceptional Performance team 在 Web 前端方面作出了卓越的贡献。广为人知的优化规则也由 13 条到 14 条&#xff0c;再到 20 条&#xff0c;乃至现在的 34 条--真是与时俱进啊。最新的 34 条也针对不同的角度做了分类。 面向内容的优化规则目前有 10 条。 1. 尽量减…