【C#4.0图解教程】笔记(第19章~第25章)
|
namespace ConsolePractice
{
class SomeClass<T1, T2>//声明一个泛型,类型参数T1,T2,也不一定要用T,可以用任意字符.
{
}
class Class2
{
static void Main()
{
var first = new SomeClass<short, int>();//构造的类型实例化
var second = new SomeClass<int, long>();//构造的类型实例化
}
}
}
|


|
namespace ConsolePractice
{
class Simple
{
static public void ReverseAndPrint<T>(T[] arr)//声明一个泛型方法,作用:数组倒序
{
Array.Reverse(arr);
foreach (T item in arr)
{
Console.Write("{0},", item.ToString());
}
Console.WriteLine();
}
}
class Program
{
static void Main()
{
var intArray = new int[] { 3, 5, 7, 9, 11 };//var也可写成int[]
var stringArray = new string[] { "first", "second", "third" };
var doubleArray = new double[] { 3.567, 7.891, 2.345 };
Simple.ReverseAndPrint<int>(intArray);//调用方法
Simple.ReverseAndPrint(intArray);//由于编译器可以从方法参数中推断类型参数,我们可以省略类型参数和调用中的尖括号
Simple.ReverseAndPrint<string>(stringArray);
Simple.ReverseAndPrint(stringArray);
Simple.ReverseAndPrint<double>(doubleArray);
Simple.ReverseAndPrint(doubleArray);
Console.ReadKey();
}
}
}
|






|
using System;
using System.Collections;
namespace ConsolePractice
{
class Program
{
static void Main()
{
int[] MyArray = { 10, 11, 12, 13 };//创建数组
IEnumerator IE = MyArray.GetEnumerator();//获取枚举数
while (IE.MoveNext())//移到下一项
{
int i = (int)IE.Current;//获取当前项
Console.WriteLine("{0}", i);//输出
}
Console.ReadKey();
}
}
}
|



|
using System;
using System.Collections;
namespace ConsolePractice
{
class ColorEnumerator : IEnumerator//继承IEnumerator接口就必须实现MoveNext(),Reset()方法和Current属性
{
string[] Colors;
int Position = -1;
public ColorEnumerator(string[] theColors)//构造函数
{
Colors = new string[theColors.Length];
for (int i = 0; i < theColors.Length; i++)
{
Colors[i] = theColors[i];
}
}
public object Current
{
get
{
if (Position == -1)
{
throw new InvalidOperationException();
}
if (Position == Colors.Length)
{
throw new InvalidOperationException();
}
return Colors[Position];
}
}
public bool MoveNext()
{
if (Position < Colors.Length - 1)
{
Position++;
return true;
}
else
return false;
}
public void Reset()
{
Position = -1;
}
}
class MyColors : IEnumerable//继承了IEnumerable就必须实现GetEnumerator()方法.
{
string[] Colors = { "Red", "Yellow", "Blue" };
public IEnumerator GetEnumerator()
{
return new ColorEnumerator(Colors);
}
}
class Program
{
static void Main()
{
MyColors mc = new MyColors();
foreach (string color in mc)
Console.WriteLine(color);
Console.ReadKey();
}
}
}
|




















|
using System;
using System.Linq;
namespace ConsolePractice
{
class Program
{
public static void Main()
{
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 4, 5, 6, 7 };
var someInts = from a in groupA
join b in groupB on a equals b
into groupAandB
from c in groupAandB//查询延续,将结果放入groupAaandB中
select c;
foreach (var a in someInts)
Console.Write("{0} ", a);
Console.ReadKey();
}
}
}
|


|
using System;
using System.Linq;
using System.Collections;
namespace ConsolePractice
{
class Program
{
public static void Main()
{
int[] intArray = new int[] { 3, 4, 5, 6, 7, 9 };
var count1 = Enumerable.Count(intArray);//直接调用
var firstnum1 = Enumerable.First(intArray);//直接调用
var count2 = intArray.Count();//扩展方法调用(数组intArray作为被扩展的对象)
var firstnum2 = intArray.First();//扩展方法调用
Console.WriteLine("Count:{0},FirstNumber:{1}", count1, firstnum1);
Console.WriteLine("Count:{0},FirstNumber:{1}", count2, firstnum1);
Console.ReadKey();
}
}
}
|









|
using System;
using System.Xml.Linq;
namespace ConsolePractice
{
class Program
{
public static void Main()
{
XDocument xd = new XDocument(
new XElement("root",
new XAttribute("color","red"),//创建时添加属性
new XAttribute("size", "large"),//创建时添加属性
new XElement("first")
)
);
Console.WriteLine(xd);//显示XML树
Console.WriteLine();//空行
XElement rt=xd.Element("root");//获取元素
XAttribute color = rt.Attribute("color");//获取属性
XAttribute size = rt.Attribute("size");//获取属性
Console.WriteLine("Color is {0}", color.Value);//显示属性值
Console.WriteLine("Size is {0}", size.Value);//显示属性值
Console.WriteLine();//空行
rt.SetAttributeValue("size", "mediun");//改变属性值
rt.SetAttributeValue("width","narrow");//添加属性,就是没有查找不到这个属性时,则添加这个属性
Console.WriteLine(xd);//显示XML树
Console.WriteLine();//空行
rt.Attribute("color").Remove();//移除属性
rt.SetAttributeValue("size", null);//移除属性,把某个属性设置为空就等于移除了.
Console.WriteLine(xd);//显示XML树
Console.ReadKey();
}
}
}
|





























【C#4.0图解教程】笔记(第19章~第25章)的更多相关文章
- 【读书笔记】关于《精通C#(第6版)》与《C#5.0图解教程》中的一点矛盾的地方
志铭-2020年2月8日 03:32:03 先说明,这是一个旧问题,很久很久以前大家就讨论了, 哈哈哈,而且先声明这是一个很无聊的问题,
- JavaScript高级程序设计(第三版)学习笔记22、24、25章
第22章,高级技巧 高级函数 安全的类型检测 typeof会出现无法预知的行为 instanceof在多个全局作用域中并不能正确工作 调用Object原生的toString方法,会返回[Object ...
- 【C#4.0图解教程】笔记(第9章~第18章)
第9章 语句 1.标签语句 ①.标签语句由一个标识符后面跟着一个冒号再跟着一条语句组成 ②.标签语句的执行完全如同标签不存在一样,并仅执行冒号后的语句. ③.给语句添加一个标签允许控制从代码的另一部分 ...
- 【C#4.0图解教程】笔记(第1章~第8章)
第1章 C#和.NET框架 1..NET框架的组成 .NET框架由三部分组成(严格来说只有CLR和FCL(框架类库)两部分),如图 执行环境称为:CLR(公共语言运行库),它在运行期管理程序的执行. ...
- C#4.0图解教程 - 第24章 反射和特性 – 2.特性
1.特性 定义 Attribute用来对类.属性.方法等标注额外的信息,贴一个标签(附着物) 通俗:给 类 或 类成员 贴一个标签,就像航空部为你的行李贴一个标签一样 注意,特性 是 类 和 类的成员 ...
- C#4.0图解教程 - 第24章 反射和特性 - 1.反射
24.1 元数据和反射 有关程序及类型的数据被成为 元数据.他们保存在程序集中. 程序运行时,可以查看其他程序集或其本身的元数据.一个运行的程序查看本身元数据或其他程序的元数据的行为叫做 反射. 24 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...
- C#温故知新:《C#图解教程》读书笔记系列
一.此书到底何方神圣? 本书是广受赞誉C#图解教程的最新版本.作者在本书中创造了一种全新的可视化叙述方式,以图文并茂的形式.朴实简洁的文字,并辅之以大量表格和代码示例,全面.直观地阐述了C#语言的各种 ...
- 《C#图解教程》读书笔记之五:委托和事件
本篇已收录至<C#图解教程>读书笔记目录贴,点击访问该目录可获取更多内容. 一.委托初窥:一个拥有方法的对象 (1)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执 ...
随机推荐
- Tomcat普通用户部署教程(生产服务器)
1.环境准备 JDK安装 解压 tar xf tomcat-xx.tar.gz -C /data/soft cd /data/soft 重命名 mv tomcat-xx tom ...
- 科大讯飞和Tizen-TTS语音合成引擎
最近在做一个文本转语音TTS(Text to Speech)的第三方软件封装,使用的是国内语音技术龙头安徽科大讯飞公司提供的离线引擎AiSound5.0,主要用于汽车导航用途.科大讯飞还提供 了AiT ...
- jsp?echo
- SaltStack管理从这里开始
Modules 1:查看所有module列表: salt 'jcfx-4' sys.list_modules jcfx-4: - acl - aliases - alternatives - apac ...
- [Objective-c 基础 - 3.3] block数据类型
A.概念 1.block类似函数 (1)可以保存代码 (2)有返回值 (3)有形参 2.block的标志:^ // 没有参数和返回值的block void (^myblock)() = ^{ // ...
- 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
package car; public class Vehicle { //定义成员变量 private int wheels; private double weight; public int g ...
- centos7 挂载ntfs移动硬盘
第一步:下载安装rpmforge ,下载地址 http://pkgs.repoforge.org/rpmforge-release/ 安装 rpm -ivh rpmforge-release-0.5 ...
- 人工智能-有限状态机(FSM)的学习
首先声明:此文源于本人最近学习的一本书 <游戏人工智能编程案例精粹> FSM的定义: 一个有限状态机是一个设备,或是一个设备模型,具有有限数量的状态,它可以在任何给定的时间根据输入进行操作 ...
- Windows 7 EXE图标丢失修复方法
有过Win7下的一些EXE文件图标莫名奇妙丢失,但功能却正常的情况吗?这是图标缓存的问题,应该是Win7的bug. 在命令提示符下输入下列命令即可恢复. 以下是代码片段: taskkill /im e ...
- 【ALearning】第三章 Android基本常见控件
本章主要介绍主要的寻常较多使用的控件,包含TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的Linea ...