【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)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执 ...
随机推荐
- [转载] Zookeeper中的 ACL(Access Control List)访问控制列表
zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...
- java@ What are C++ features missing in Java
Following features of C++ are not there in Java. No pointers No sizeof operator No scope resolution ...
- [NOI2005]维修数列 Splay tree 区间反转,修改,求和,求最值
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 Description Input 输入文件的第1行包含两个数N和M,N表示初始时数 ...
- QQMusic绿钻兄,你可安好?我需要晴天。
不好意思,年纪这样大了,还依靠吐槽来保持呆毛的正能量,实在对不住,先说对不起. QQMusic是我最喜欢的腾讯增值服务,正版内容,海量歌手,高清下载.实在是音乐软件中高大上的典范,除了歌手排名中前十中 ...
- 转载Sql 获取数据库所有表及其字段名称,类型,长度
转载原地址 http://www.cnblogs.com/Fooo/archive/2009/08/27/1554769.html SELECT (case when a.colorder=1 the ...
- [struts2]jstl标签用法技巧
1.<c:if test="${var} != null"></c:if> 2. <c:foreach var="singleVar&quo ...
- 重看Decorator Pattern,联想到Delegate传递及Flags Enum--欢迎拍砖!
话说装饰模式(Decorator)的动机是“动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活.[GOF <设计模式>]”.再次学到该模式,有感 ...
- RStudio:R语言编辑器
RStudio:R语言编辑器 四窗口 左上:写代码,运行的方式是ctrl+Enter,或者用Run按钮 10 + 15 ## [1] 25 左下:终端,上面窗口的代码运行后会在这里显示,也可以直接在这 ...
- Linux下JDK安装位置
新手在Linux上安装JDK时,不知道应该将JDK安装在哪比较合适.首先简要了解下Linux中部分目录的作用. /bin---用来贮存用户命令./usr/bin 也被用来贮存用户命令. /sbin- ...
- 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)
从Spring框架流行后,几乎根本不用自己写解析配置文件的代码了,但近日一个基础项目(实在是太基础,不能用硕大繁琐的Spring), 碰到了用java.util.Properties读取中文内容(UT ...