【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)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执 ...
随机推荐
- Java中使用ThreadPoolExecutor并行执行独立的单线程任务
Java SE 5.0中引入了任务执行框架,这是简化多线程程序设计开发的一大进步.使用这个框架可以方便地管理任务:管理任务的生命周期以及执行策略. 在这篇文章中,我们通过一个简单的例子来展现这个框架所 ...
- 射频识别技术漫谈(4)——数据编码【worldsing 笔记】
前已述及,射频识别技术中的调制方法一般使用调幅(AM),也就是将有用信号调制在载波的幅度上传送出去.这里的"有用信号"指用高低电平表示的数据"0"或" ...
- (10.09作业)学生选课数据库SQL语句练习题
- 解决ecshop在线客户点击无法唤醒QQ问题
找到default/library/page_footer.lbi中找到QQ代码的相应位置,然后你会发现之前模板里面为什么QQ点击不能对话,是因为QQ客服安装包中的JS代码有的可能是比较旧的代码了. ...
- Swif基本语法以及与OC比较三
(未 经 博 主 同 意,不 得 转 载 !) ------------------------华丽分割线----------------------- // // main.swift ...
- How good software makes us stupid?
How good software makes us stupid? 科技是怎样让人变傻的? People assume that iPhones, laptops and Netflix are e ...
- 【转】Netty那点事(二)Netty中的buffer
[原文]https://github.com/code4craft/netty-learning/blob/master/posts/ch2-buffer.md 上一篇文章我们概要介绍了Netty的原 ...
- 写了几年代码了,苦苦追寻,应该沉淀下来了,好好研究。net底层框架,以及较好的分层框架
几年码农了.像沉淀下来.写一下自己的分层框架,尤其是逻辑层和orm层.数据訪问层.一切靠自己.网上一大堆框架,可是感觉各有优缺点.于是萌生了自己写适合自己的底层訪问框架?亲们,你们有适合自己的框架么?
- windows环境下搭建ffmpeg开发环境
ffmpeg是一个开源.跨平台的程序库,能够使用在windows.linux等平台下,本文将简单解说windows环境下ffmpeg开发环境搭建过程,本人使用的操作系统为windows ...
- ArcGIS教程:加权叠加
摘要 使用经常使用測量比例叠加多个栅格数据,并依据各栅格数据的重要性分配权重. 插图 插图中,两个输入栅格已又一次分类为 1 至 3 三种公共測量级别.为每一个栅格均分配了一个影响百分比.这些像元值与 ...