C#高级编程笔记 (1至6章节)数组,类/方法/泛型
2.3变量
var 类型推断
type 类的分类 如:type nametype = name.GetType(); //取变量name的类型
const 常量 const int painame = 3.1415
char 是字符类型 string是字符串类型
2.5语句
选择语句:switch(变量){case 常量值1:语句1 break;…………;default 常量值n:语句2 break;}
二个值相同语句时:switch(变量){case 常量值1:case 常量值1:语句1 break;…………;default 常量值n:语句2 break;}
wile 循环一般用在不知道循环多少次的情况下
do…wile 是先循环后判断
foreach() 遍历
goto 跳转语句 goto pray;语句1;…………;pray: 语句n; ///直接跳过语句1执行语句n (大多情况下不允许用慎用)
break 跳出循环体
continue 跳过当次循环(非跳出循环体)
2.6枚举型
- Main()
- {
- TimeOfDay time = (TimeOfDay)Enum.Parse(typeof(TimeOfDay), "afternoon",true); //从字符串对应的值,参数3区分大小写
- Console.WriteLine((int)time);
- TimeOfDay time2 = TimeOfDay.Evening;
- Console.WriteLine((int)time2);//输出为2
- }
- public enum TimeOfDay
- {
- morning = ,
- Afternoon = ,
- Evening = ,
- }
2.7空间名称
- testpray.testclass.Class1.***()
- namespace testpray
- {
- namespace testclass {
- class Class1
- {}}}
2.8给Main()方法传递参数
- static int Main(string[] args)
- {
- for (int i = ; i < args.Length; i++)
- {
- Console.WriteLine(args[i]);
- }
- return ;
- }
- //在CMD下运行程序 test /a /r /e ,a、r、e为参数
2.10控制台
Console.WriteLine("{0,4:C2}\n",i,} // 0表示索引值(对应变量I),4表示宽度(正直表示右对齐,负值表示左对齐,C2表输出精度后2位的货币,)
C | 货币 |
D | 十进制 |
E | 科学计数法 |
F | 小数点位数 |
G | 普通格式 (只显示前n个有效数字) |
N | 显示千分制 12543.234 n2 12,543.12 |
p | 百分数 0.52123 p2 52.12% |
X | 十六进制 |
2.12 C#预处理指令
#define 名称符号 (声明存在这个符号,用于调试) #undef 名称符号 (删除符号)
#if 名称符号 (在前面的#define时才会执行) #
3.3 类
类的参数,如果是引用类型(如数组或类)传递给方法,对应的方法会改变数组中的值,而新值会反射在原数组里.
ref 参数,在值参数前加入ref 其方法对参数的改变会在原始对象中体现!(调用时也要在参数上添加 ref)
out 参数,传递时变量可以不初始化,(调用时也要在参数前加 out)
命名参数,调用时可 test(i:32,98);
可选参数,必须在是方法定义的最后的参数(net4) 例:void test(int i,int n = 10) {…} //多个可选参数时可与命名参数指定参数!
方法的重载
属性的访问修饰符如: public name{get{} private set{}}
(构造函数???)
struct 定义一个结构体 如struct 名称 {} 注:结构是值类型
partial 部分类 ,在多个文件中存放有方法和属性 例: partial class name{}
3.10 扩展方法
扩展方法是静态方法 新建一个静态类,然后其内容中添加:
public static void AddToAmount(this 类名 参数1,扩展的方法) {参数1.Amount += 扩展的方法 }
4.3.1虚方法 (用于多态性)
virtual 关键字,例 基类: public virtual string Testprint(){} 派生类:public override string Testprint()注:字段与静态函数不能声明为virtual
- using System;
- namespace PolymorphismApplication
- {
- class Shape
- {
- protected int width, height;
- public Shape( int a=, int b=)
- {
- width = a;
- height = b;
- }
- public virtual int area()
- {
- Console.WriteLine("父类的面积:");
- return ;
- }
- }
- class Rectangle: Shape
- {
- public Rectangle( int a=, int b=): base(a, b)
- {
- }
- public override int area ()
- {
- Console.WriteLine("Rectangle 类的面积:");
- return (width * height);
- }
- }
- class Triangle: Shape
- {
- public Triangle(int a = , int b = ): base(a, b)
- {
- }
- public override int area()
- {
- Console.WriteLine("Triangle 类的面积:");
- return (width * height / );
- }
- }
- class Caller
- {
- public void CallArea(Shape sh)
- {
- int a;
- a = sh.area();
- Console.WriteLine("面积: {0}", a);
- }
- }
- class Tester
- {
- static void Main(string[] args)
- {
- Caller c = new Caller();
- Rectangle r = new Rectangle(, );
- Triangle t = new Triangle(, );
- c.CallArea(r);
- c.CallArea(t);
- Console.ReadKey();
- }
- }
- }
实例:虚方法的多态性
4.3.2隐藏方法要隐藏基类时要用New
调用基类的方法 base.基类函数名()
抽象类与方法 必须在派生类中重写, 理解用于防漏写方法 修饰符:abstract
密封类与方法 例 sealed class name{} 对于类表示不能继承, 对于方法表示不能重写
注:应用于方法或属性时,sealed 修饰符必须始终与 override 结合使用。
4.36 带参数的构造函数 this()表示当前法
4.5 接口 关键字:interface 例 public interface IDisposable{} 有点类类似抽象类 接口的应用统一接口 相关介绍
派生的接口例:(如不理解请查看108页与相关介绍)
- public interface ITransferBankAccount : IBankAccount
- {
- bool TransferTo(IBankAccount destination, decimal amount);
- }
5.1.1泛型 相关介绍与用法可参考
5.3.2约束 where T :struct 对于结构约束,类型T必须是值类型
5.4.1 协变与抗变 相关资料 相关理解
- static void Main(string[] args)
- {
- //协变
- IIndex<Rectangle> rectangles = RectangleCollection.GetRectangles();
- IIndex<Shape> shapes = rectangles;
- for (var i = ; i < shapes.Count; i++)
- {
- Console.WriteLine(shapes[i]);
- }
- Console.ReadKey();
- }
- }
- public class Shape
- {
- public double Width { get; set; }
- public double Height { get; set; }
- public override string ToString()
- {
- return string.Format("Width: {0}, Height: {1}.", Width.ToString(), Height.ToString());
- }
- }
- public class Rectangle : Shape
- {
- }
- public interface IIndex<out T>
- {
- T this[int index] { get; }
- int Count { get; }
- }
- public class RectangleCollection : IIndex<Rectangle>
- {
- private readonly Rectangle[] _data = new Rectangle[]
- {
- new Rectangle{Height = ,Width = },
- new Rectangle{Height = ,Width = },
- new Rectangle{Height = ,Width = },
- };
- public static RectangleCollection GetRectangles()
- {
- return new RectangleCollection();
- }
- public Rectangle this[int index]
- {
- get
- {
- if (index < || index > _data.Length)
- throw new ArgumentOutOfRangeException("index");
- return _data[index];
- }
- }
- public int Count { get { return _data.Length; } }
协变实例
6.4锯齿数组
int[][] =new int[3],[]
6.5Array类 如果数组元素超出整数取值范围,可以做用longlength属性来获取元素大小,,,Rank属性-数组的维数
6.5.1创建数组 Array1.CreateInstance(typeof(int),5)
设置数组元素Array1.SetValue(元素值,下标); 获取元素Array1.GetValue(下标);
6.5.2复制数组 因数组为引用类型,所以不能直接用等号,如果数组的元素为值类型,复制用 Array2=(int[]) Array1.Clone(); 注意如果数组的元素为引用类型则只复制引用,修改其只一个会改变另一个对象,如要深层副本,则必须迭代数组并创建新对象。
6.5.3数组排序 简单类型int与String可直接Array.Sort(数组)排序,如自定义类, 要使之实现IComparable<类>接口的Compareto方法,如下:
- public int CompareTo(Person other)
- {
- if (other == null) throw new ArgumentNullException("other");
- int result = this.LastName.CompareTo(other.LastName);
- if (result == )
- {
- result = this.FirstName.CompareTo(other.FirstName);
- }
- return result;
- }
6.6.2 ArraySegment<T>
- static void Main()
- {
- int[] ar1 = { , , , , , };
- int[] ar2 = { , , , , , , };
- //分隔ar1的索引0后的3个元素,与ar2的索引3后的3个元素.
- var segments = new ArraySegment<int>[] {new ArraySegment<int>(ar1, , ), new ArraySegment<int>(ar2, , )};
- var sum = SumOfSegments(segments);
- Console.WriteLine("sum of all segments: {0}", sum);
- Console.ReadKey();
- }
- static int SumOfSegments(ArraySegment<int>[] segments)
- {
- int sum = ;
- foreach (var segment in segments)
- {
- for (int i = segment.Offset; i < segment.Offset + segment.Count; i++)
- //i取的是数组的原数组下标值!即ar1与ar2的下标
- {
- sum += segment.Array[i];
- }
- }
- return sum;
- }
如数组段中改变了数组,则原数组也会改变!
6.7.3yield语句(yield是一个语法糖)
1迭代集合不同方式
- class Program
- {
- static void Main()
- {
- var titles = new MusicTitles();
- foreach (var title in titles.Subset(, ))
- {
- Console.WriteLine(title);
- }
- }
- }
- public class MusicTitles
- {
- string[] names = { "Tubular Bells", "Hergest Ridge", "Ommadawn", "Platinum" };
- public IEnumerable<string> Subset(int index, int length)
- {
- for (int i = index; i < index + length;
- i++)
- {
- yield return names[i];
- }
- }
- }
2用yieid return 返回枚举器
- static void Main()
- {
- var game = new GameMoves();
- IEnumerator enumerator = game.Cross();
- while (enumerator.MoveNext())
- {
- enumerator = enumerator.Current as IEnumerator;
- }
- Console.ReadLine();
- }
- public class GameMoves
- {
- private IEnumerator cross;
- private IEnumerator circle;
- public GameMoves()
- {
- cross = Cross();
- circle = Circle();
- }
- private int move = ;
- const int MaxMoves = ;
- public IEnumerator Cross()
- {
- while (true)
- {
- Console.WriteLine("Cross, move {0}", move);
- if (++move >= MaxMoves)
- yield break;
- yield return circle;//下一次为circle方法
- }
- }
- public IEnumerator Circle()
- {
- while (true)
- {
- Console.WriteLine("Circle, move {0}", move);
- if (++move >= MaxMoves)
- yield break;
- yield return cross;//下一次为Cross方法
- }
- }
- }
6.8元组
- var result = Divide(, );
- Console.WriteLine("result of division: {0}, reminder: {1}, resum:{2}", result.Item1, result.Item2, result.Item3);
- public static Tuple<int, int,int> Divide(int dividend, int divisor)
- {
- int result = dividend / divisor;
- int reminder = dividend % divisor;
- int resum = dividend + divisor;
- return Tuple.Create<int,int,int>(result, reminder, resum);
6.9结构比较
通过类IStructuralEquatable接口实现比较内容与引用!
对于IstructuralEquatable接口定义的Equals()方法,它的第一个参数是object类型,
第二个参数是IEqualityComparer类型。调用这个方法时,通过传递一个实现了IEqualityComparer<T>的对象,就
可以定义如何进行比较。通过EqualityComparer<T>类完成IEqualityComparer的一个默认实现。这个实现检查类型是否
实现了IEquatable接口,并调用IEquatable.Equals()方法(即这里的Person类的Equals方法)。如果该类型没有实现IEquatable,就
调用Object基类中的Equals()方法进行比较。
if ((persons1 as IStructuralEquatable).Equals(
persons2, EqualityComparer<Person>.Default))
单步调试上面这行代码时,由于persons1和persons2共有三个元素
所以比较函数会进行三次(注意:会被执行三次),由于两个数组中第三个元素的FirstName
不相同,所以返回内容不相同的结果
注意:如果两个数组的元素个数不相等,则直接返回不相等,不会调用Person类的Equals比较方法
- static void Main()
- {
- var janet = new Person { FirstName = "Janet", LastName = "Jackson"};
- Person[] persons1 = { new Person { FirstName = "Michael", LastName = "Jackson" }, janet };
- Person[] persons2 = { new Person { FirstName = "Michae2", LastName = "Jackson" }, janet };
- Console.WriteLine(janet.ToString());
- Console.ReadLine();
- if (persons1 != persons2) {
- Console.WriteLine("不相同的引用");
- }
- if (!persons1.Equals(persons2))
- Console.WriteLine("等于返回false -不相同的引用");
- if ((persons1 as IStructuralEquatable).Equals(persons2, EqualityComparer<Person>.Default))
- Console.WriteLine("相同的内容");
- }
- public class Person : IEquatable<Person>
- {
- public int Id { get; private set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public override string ToString()
- {
- return String.Format("{0}, {1} {2}", Id, FirstName, LastName);
- }
- public bool Equals(Person other)
- {
- if (other == null)
- return base.Equals(other);
- return this.FirstName == other.FirstName && this.LastName == other.LastName;
- }
- }
C#高级编程笔记 (1至6章节)数组,类/方法/泛型的更多相关文章
- C#高级编程笔记 (6至10章节)运算符/委托/字符/正则/集合
数学的复习,4^-2即是1/4/4的意思, 4^2是1*2*2的意思,而10^-2为0.01! 7.2运算符 符号 说明 例 ++ 操作数加1 int i=3; j=i++; 运算后i的值为4,j ...
- C#高级编程笔记(22至25章节)文件\注册表\权限\事务
22安全(using System.Security.Principal;) AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.Wi ...
- C#高级编程笔记(17至21章节)线程/任务
17 Visual Studio 2013 控制台用Ctrl+F5可以显示窗口,不用加Console.ReadLine(); F5用于断点调式 程式应该使用发布,因为发布的程序在发布时会进行优化, 2 ...
- Android高级编程笔记(四)深入探讨Activity(转)
在应用程序中至少包含一个用来处理应用程序的主UI功能的主界面屏幕.这个主界面一般由多个Fragment组成,并由一组次要Activity支持.要在屏幕之间切换,就必须要启动一个新的Activity.一 ...
- UNIX环境高级编程笔记之文件I/O
一.总结 在写之前,先唠几句,<UNIX环境高级编程>,简称APUE,这本书简直是本神书,像我这种小白,基本上每看完一章都是“哇”这种很吃惊的表情.其实大概三年前,那会大三,我就买了这本书 ...
- javascript高级编程笔记01(基本概念)
1.在html中使用JavaScript 1. <script> 元素 <script>定义了下列6个属性: async:可选,异步下载外部脚本文件. charset:可选, ...
- C#高级编程笔记之第三章:对象和类型
类和结构的区别 类成员 匿名类型 结构 弱引用 部分类 Object类,其他类都从该类派生而来 扩展方法 3.2 类和结构 类与结构的区别是它们在内存中的存储方式.访问方式(类似存储在堆上的引用类型, ...
- C#高级编程笔记之第二章:核心C#
变量的初始化和作用域 C#的预定义数据类型 流控制 枚举 名称空间 预处理命令 C#编程的推荐规则和约定 变量的初始化和作用域 初始化 C#有两个方法可以一确保变量在使用前进行了初始化: 变量是字段, ...
- C#高级编程笔记(11至16章)异步/托管/反射/异常
11.1.2LINQ语句 LINQ查询表达式以from子句开始,以select或者group子句结束.在这两个子句之间可以跟零个或者多个from.let.where.join或者orderby子句. ...
随机推荐
- 【CF1243B1】Character Swap (Easy Version)【思维】
题意:给你两个字符串,问是否存在交换方案使得两个字符串变成一样的,方案为只交换一次且只交换s1与s2里的一个字符 题解:若一开始就相同,则存在交换方案 若一开始不同的位置为1个或大于2个,则不存在方案 ...
- vim 批量添加删除注释
vim中单行注释只是多行注释的一个特例,这里统一进行多行注释的讲解 (1)添加批量注释 ctrl+v 进入列编辑模式,向下或向上移动光标,把需要注释的行的开头标记起来,然后按大写的I(shift+i) ...
- linux如何查看端口被哪个进程占用的方法
linux如何查看端口被哪个进程占用的方法: 1.lsof -i:端口号2.netstat -tunlp|grep 端口号 都可以查看指定端口被哪个进程占用的情况[步骤一]lsof -ilsof -i ...
- leetcode 235. 二叉搜索树的最近公共祖先(c++)
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...
- Nginx 实现全站 HTTPS(基于 Let's Encrypt 的免费通配符证书)
单域名证书的生成可以 参考这里. acme.sh 项目中文文档 Let's Encrypt 在 18 年 1 月份推出了 ACME v2,支持通配符域名证书,对小网站.个人站长的友好度进一步增加. 常 ...
- oracle两表中的两列进行模糊匹配的方法
SELECT T2.列名,T1.列名 FROM 主表 T1, 匹配表 T2 WHERE T1.匹配列 LIKE CONCAT('%',concat(T2.匹配列,'%')); 注意: a ...
- java文件上传下载 使用SmartUpload组件实现
使用SmartUpload组件实现(下载jsmartcom_zh_CN.jar) 2017-11-07 1.在WebRoot创建以下文件夹,css存放样式文件(css文件直接拷贝进去),images存 ...
- Throwable -抛出异常类与自定义异常类
/* 自定义异常类 java提供的异常类,不够我们使用,需要自己定义一些异常类 格式: public class XXXException extends Exception/runtimeExcep ...
- Spring Security 04
转至:Elim的博客http://elim.iteye.com/blog/2161648 Filter Porxy DelegatingFilterProxy DelegationFilterProx ...
- [Linux] 026 光盘 yum 源搭建
光盘 yum 搭建步骤 (1) 挂载光盘 $ mount /dev/cdrom /mnt/cdrom/ (2) 让网络 yum 源文件失效 $ cd /etc/yum.repos.d/ $ mv Ce ...