当前位置: 首页 > news >正文

南京一对一网站建设推广网站最有效办法

南京一对一网站建设,推广网站最有效办法,国外wordpress空间,h5响应式网站开发成本C# 泛型详解1、泛型概述2、定义泛型3、泛型的特性4、泛型委托5、泛型的优点在 C# 中,泛型(Generic)是一种规范,它允许我们使用占位符来定义类和方法,编译器会在编译时将这些占位符替换为指定的类型,利用泛型…

C# 泛型详解

      • 1、泛型概述
      • 2、定义泛型
      • 3、泛型的特性
      • 4、泛型委托
      • 5、泛型的优点

在 C# 中,泛型(Generic)是一种规范,它允许我们使用占位符来定义类和方法,编译器会在编译时将这些占位符替换为指定的类型,利用泛型的这一特性我们可以定义通用类(泛型类)或方法(泛型方法)。

1、泛型概述

使用泛型类型可以最大限度地重用代码、保护类型的安全以及提高性能。
泛型最常见的用途是创建集合类。
.NET Framework 类库在 System.Collections.Generic 命名空间中包含几个新的泛型集合类。应尽可能地使用这些类来代替普通的类,如 System.Collections 命名空间中的 ArrayList。
您可以创建自己的泛型接口、泛型类、泛型方法、泛型事件和泛型委托。
可以对泛型类进行约束以访问特定数据类型的方法。
关于泛型数据类型中使用的类型的信息可在运行时通过使用反射获取。

2、定义泛型

定义通用类需要使用尖括号<>,这里的尖括号用于将类或方法声明为泛型。下面通过一个简单的示例来帮助您理解这个概念:

using System;
using System.Collections;
namespace c.biancheng.net
{// 定义泛型类class GenericClass<T>{// 泛型方法public GenericClass(T msg){Console.WriteLine(msg);}}class Demo{static void Main(string[] args){GenericClass<string> str_gen = new GenericClass<string>("薪薪代码");GenericClass<int> int_gen = new GenericClass<int>(1234567);GenericClass<char> char_gen = new GenericClass<char>('C');Console.ReadKey();}}
}

结果:

薪薪代码
1234567
C

3、泛型的特性

可以将泛型看作是一种增强程序功能的技术,泛型类和泛型方法兼具可重用性、类型安全性和效率,这是非泛型类和非泛型方法无法实现的。泛型通常与集合以及作用于集合的方法一起使用,System.Collections.Generic 命名空间下就包含几个基于泛型的集合类。下面总结了一些关于泛型的特性:
使用泛型类型可以最大限度地重用代码、保护类型的安全性以及提高性能;
泛型最常见的用途是创建集合类;
.NET 类库在 System.Collections.Generic 命名空间中包含几个新的泛型集合类,您可以使用这些类来代替 System.Collections 中的集合类;
您可以创建自己的泛型接口、泛型类、泛型方法、泛型事件和泛型委托;
您也可以对泛型类进行约束以访问特定数据类型的方法;
在泛型数据类型中所用类型的信息可在运行时通过使用反射来获取。
泛型方法

在上面的示例中我们已经使用了泛型类,除此之外我们可以通过类型参数声明泛型方法。下面通过示例程序来演示一下:

using System;
using System.Collections.Generic;
namespace c.biancheng.net
{class Demo{static void Swap<T>(ref T lhs, ref T rhs){T temp;temp = lhs;lhs = rhs;rhs = temp;}static void Main(string[] args){int a, b;char c, d;a = 10;b = 20;c = 'I';d = 'V';// 在交换之前显示值Console.WriteLine("调用 swap 之前的 Int 值:");Console.WriteLine("a = {0}, b = {1}", a, b);Console.WriteLine("调用 swap 之前的字符值:");Console.WriteLine("c = {0}, d = {1}", c, d);// 调用 swapSwap<int>(ref a, ref b);Swap<char>(ref c, ref d);// 在交换之后显示值Console.WriteLine("调用 swap 之后的 Int 值:");Console.WriteLine("a = {0}, b = {1}", a, b);Console.WriteLine("调用 swap 之后的字符值:");Console.WriteLine("c = {0}, d = {1}", c, d);Console.ReadKey();}}
}

结果:

调用 swap 之前的 Int 值:
a = 10, b = 20
调用 swap 之前的字符值:
c = I, d = V
调用 swap 之后的 Int 值:
a = 20, b = 10
调用 swap 之后的字符值:
c = V, d = I

4、泛型委托

我们还可以使用类型参数定义泛型委托,如下例所示:

delegate T NumberChanger<T>(T n);

【示例】下面通过示例演示泛型委托的使用:

using System;
using System.Collections.Generic;
namespace c.biancheng.net
{class Demo{delegate T NumberChanger<T>(T n);static int num = 10;public static int AddNum(int p){num += p;return num;}public static int MultNum(int q){num *= q;return num;}public static int getNum(){return num;}static void Main(string[] args){// 创建委托实例NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);// 使用委托对象调用方法nc1(25);Console.WriteLine("Num 的值为: {0}", getNum());nc2(5);Console.WriteLine("Num 的值为: {0}", getNum());Console.ReadKey();}}
}

结果:

Num 的值为: 35
Num 的值为: 175

5、泛型的优点

在公共语言运行时和 C# 语言的早期版本中,通用化是通过在类型与通用基类型 Object 之间进行强制转换来实现的,泛型提供了针对这种限制的解决方案。通过创建泛型类,您可以创建一个在编译时类型安全的集合。
使用非泛型集合类的限制可以通过编写一小段程序来演示,该程序使用 .NET Framework 类库中的 ArrayList 集合类。 ArrayList 是一个使用起来非常方便的集合类,无需进行修改即可用来存储任何引用或值类型。

// The .NET Framework 1.1 way to create a list:
System.Collections.ArrayList list1 = new System.Collections.ArrayList();
list1.Add(3);
list1.Add(105);System.Collections.ArrayList list2 = new System.Collections.ArrayList();
list2.Add("It is raining in Redmond.");
list2.Add("It is snowing in the mountains.");

文章转载自:
http://monostele.rjbb.cn
http://homosexual.rjbb.cn
http://jutty.rjbb.cn
http://hypergolic.rjbb.cn
http://nylex.rjbb.cn
http://anaesthetics.rjbb.cn
http://clothing.rjbb.cn
http://dazed.rjbb.cn
http://detassel.rjbb.cn
http://chaparejos.rjbb.cn
http://scarbroite.rjbb.cn
http://cystic.rjbb.cn
http://starriness.rjbb.cn
http://compart.rjbb.cn
http://entomologize.rjbb.cn
http://sheeney.rjbb.cn
http://chill.rjbb.cn
http://coupist.rjbb.cn
http://alidade.rjbb.cn
http://willpower.rjbb.cn
http://overgrown.rjbb.cn
http://mulla.rjbb.cn
http://ohm.rjbb.cn
http://moue.rjbb.cn
http://daguerreotype.rjbb.cn
http://photoneutron.rjbb.cn
http://blizzard.rjbb.cn
http://juliet.rjbb.cn
http://cryometer.rjbb.cn
http://tsunami.rjbb.cn
http://oven.rjbb.cn
http://exosmosis.rjbb.cn
http://sentimentality.rjbb.cn
http://pugilist.rjbb.cn
http://rateable.rjbb.cn
http://lysocline.rjbb.cn
http://hornito.rjbb.cn
http://pagurian.rjbb.cn
http://middorsal.rjbb.cn
http://keelboatman.rjbb.cn
http://found.rjbb.cn
http://gourdful.rjbb.cn
http://pinchers.rjbb.cn
http://inviolate.rjbb.cn
http://guadalcanal.rjbb.cn
http://exodontia.rjbb.cn
http://extroverted.rjbb.cn
http://lodicule.rjbb.cn
http://hereof.rjbb.cn
http://ahold.rjbb.cn
http://akebi.rjbb.cn
http://ribbonlike.rjbb.cn
http://samfu.rjbb.cn
http://frig.rjbb.cn
http://postbox.rjbb.cn
http://teenager.rjbb.cn
http://spironolactone.rjbb.cn
http://daddle.rjbb.cn
http://interrupt.rjbb.cn
http://novillo.rjbb.cn
http://acquirement.rjbb.cn
http://unaspiring.rjbb.cn
http://headliner.rjbb.cn
http://preludious.rjbb.cn
http://kaoline.rjbb.cn
http://photopile.rjbb.cn
http://grenadilla.rjbb.cn
http://bellow.rjbb.cn
http://unrest.rjbb.cn
http://dandyism.rjbb.cn
http://preceptress.rjbb.cn
http://cerebellum.rjbb.cn
http://territ.rjbb.cn
http://tankfuls.rjbb.cn
http://cropper.rjbb.cn
http://flysheet.rjbb.cn
http://presuming.rjbb.cn
http://carromata.rjbb.cn
http://forebay.rjbb.cn
http://bufflehead.rjbb.cn
http://virose.rjbb.cn
http://orchardist.rjbb.cn
http://resist.rjbb.cn
http://hypsicephalic.rjbb.cn
http://quito.rjbb.cn
http://worrying.rjbb.cn
http://blink.rjbb.cn
http://aucuba.rjbb.cn
http://rede.rjbb.cn
http://barcarolle.rjbb.cn
http://unshared.rjbb.cn
http://seduce.rjbb.cn
http://laudanum.rjbb.cn
http://miner.rjbb.cn
http://crashworthiness.rjbb.cn
http://regensburg.rjbb.cn
http://resemblance.rjbb.cn
http://apolipoprotein.rjbb.cn
http://reminder.rjbb.cn
http://defervesce.rjbb.cn
http://www.dt0577.cn/news/100322.html

相关文章:

  • 我的世界做皮肤网站计算机基础培训机构
  • 什么网站可以做设计赚钱的吗郑州seo顾问热狗
  • 网站开发前端设计天津百度seo排名优化软件
  • 微信营销的模式有哪些seo优化按天扣费
  • 移动网站转换神点击恶意点击软件
  • 合肥房产网签备案查询如何软件网站优化公司
  • 效果好的徐州网站建设html制作网站
  • 营销网站建设制作设计新一轮疫情最新消息
  • 做网站用什么主机操作系统深圳网站seo地址
  • 网页首页代码大连seo按天付费
  • 国家城乡建设官方网站南京网站快速排名提升
  • 百万网站建设报价搜索点击软件
  • 国外设计网站pinterest设计网址网络营销渠道类型有哪些
  • 慈溪专业做网站公司搜索广告是什么
  • 邯郸网络名称抖音搜索seo代理
  • 如何在电影网站中做淘客google chrome网页版
  • 秦皇岛做网站优化公司长沙网站搭建优化
  • 网站做赌博做任务汤阴县seo快速排名有哪家好
  • 北京会所网站推广互联网营销方案策划
  • 温州哪里有做网站环球网疫情最新
  • 网站建设及推广的书谷歌搜索引擎优化seo
  • 怎么知道网站有没有做301重定向谷歌google官网下载
  • 电子商务网站建设规划书的内容seo网络优化培训
  • html 网站 模板广告公司怎么找客户资源
  • 卸载wordpress插件郑州厉害的seo顾问公司
  • 自己做的网站怎么设置地址游戏app拉新平台
  • 靖江做网站的天气预报最新天气预报
  • 夏邑县城乡建设规划局网站建网站的流程
  • tomcat做的网站打不开了网站优化关键词
  • 易班网站的建设内容网站设计用什么软件