【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)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执 ...
随机推荐
- MapReduce之Partition的使用与分析
Partition主要作用就是将map的结果发送到相应的reduce.这就对partition有两个要求: 1)均衡负载,尽量的将工作均匀的分配给不同的reduce. 2)效率,分配速度一定要快. M ...
- leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)
https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...
- Android Studio 模拟器启动问题——黑屏 死机 解决方法
今天用了下Android Studio,出现了一些问题,现在将启动过程中遇到的问题和解决方案列出来,方便大家参考. 安装过程不多说,网上一搜一大把. 那直接说问题吧: 1. 无法启动,报错:Faile ...
- 转载Code First Migrations更新数据库架构的具体步骤
[转载] Code First Migrations更新数据库结构的具体步骤 我对 CodeFirst 的理解,与之对应的有 ModelFirst与 DatabaseFirst ,三者各有千秋,依项 ...
- Keil Mdk5.0 破解包 和谐包【worldsing笔记】
有关Keil MDK 5.0的介绍和下载 http://www.cnblogs.com/worldsing/p/3355911.html 下载地址 点击下载:http://pan.baidu.com/ ...
- PowerDesigner 模型文档 说明
PowerDesigner 模型文档 说明 目录(?)[+] 一. 模型文档说明 在前面几篇里介绍了PowerDesigner 的几种模型,如果我们项目里用到的模型较多,亦或者项目牵涉的部门很 ...
- Android开发日志问题
以前在Android开发中发现,日志打印好多,调试的时候各种加Log,之后就各种不删除,导致项目后期花大把时间删除日志打印. 学到一个好方法: 在所有尽可能高的父类里面加上一个常量 DEBUG ,一开 ...
- webservice 地址
快递查询WEB服务 http://webservice.36wu.com/ExpressService.asmx 支持上百家快递/物流查询,准确高效,所有数据均来自快递服务商.此数据返回类型进行了封装 ...
- <转>linux 下stm32开发环境安装
传送门: http://www.eefocus.com/marianna/blog/13-10/298454_7e04f.html http://blog.sina.com.cn/s/blog_643 ...
- IOS设置button 图片 文字 上下、左右
[btn setImage:imgNor forState:UIControlStateNormal]; [btn setImage:imgSel forState:UIControlStateSel ...