【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)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执 ...
随机推荐
- [解决]ASP.NET MVC 4/5 源码调试(source code debug)
========================ASP.NET MVC 4============================ ASP.NET MVC 4 source code download ...
- HW7.1
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- tomcat6-7配置管理用户
tomcat6: <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename=" ...
- zabbix邮件报警脚本(Python)
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...
- Go2Shell
1.背景 windows系统可以轻而易举地拿到文件所在目录, 但是mac显得想拿文件目录有点蛋疼.而Go2Shell可以快速定位到文件所在的目录. 2.安装配置 选择默认打开的终端软件 3.使用 进入 ...
- innobackupex 单脚本循环7天一全备6增备脚本更新
#!/bin/bash #日期转为天数 function date2days { echo "$*" | awk '{ z=-$)/); y=$+-z; m=$+*z-; j=*m ...
- <转>使用eclipse编译cocos2d-x示例项目,创建cocos2d-x android项目并部署到真机
准备 今天将cocos2d-x的示例项目tests编译到android真机运行,以及如何创建cocos2d-x的android项目. 打开cocos2d-x的tests项目,路径为:D:\cocos2 ...
- 【转】移动前端手机输入法自带emoji表情字符处理
http://blog.csdn.net/binjly/article/details/47321043 今天,测试给我提了一个BUG,说移动端输入emoji表情无法提交.很早以前就有思考过,手机输入 ...
- Hive权限介绍
一.开启权限 眼下hive支持简单的权限管理,默认情况下是不开启.这样全部的用户都具有同样的权限.同一时候也是超级管理员.也就对hive中的全部表都有查看和修改的权利,这样是不符合一般数据仓库的安全原 ...