c#面向对象 封装继承多态 字符串
概念 语法 属性 静态与非静态 构造函数 this方法 析构方法
命名空间 类型引用 字符串法法 继承
里氏语法转换 protect Arralist Hashtable 繁简转化 Path File类 编码
File文件读写 List泛型结合 装箱拆箱 字典集合 FileStream StreamReader和StreamWriter 多态 抽象 模拟移动营盘
c#访问修饰符 简单工厂模式 值的传递与引用 序列化与反序列化 密封类 重写父类 接口
属性
//属性 没有() public String Name { get {return _name;} set {_name = value;} }
class Person { public String _name; public int _age; public char _gender;
//属性 没有() public String Name { get {return _name;} set {_name = value;}
public void CHWL() { Console.WriteLine("我是{0}今年{1}岁",this._name,this._age); Console.WriteLine("吃喝玩乐!!!"); Console.ReadKey(); } }
---------------------------------------------------------
namespace project
{
class Program
{
static void Main(string[] args)
{
Person huLuWa = new Person();
//huLuWa._name = "葫芦娃";
huLuWa.Name=("葫芦娃"); //输入时会自动调用set
huLuWa._age = 999;
huLuWa._gender = '植';
huLuWa.CHWL();
Console.WriteLine(huLuWa.Name); //输出时会自动调用 get
Console.ReadKey();
}
}
}
private String _name private int _age
静态与非静态 静态类(工具栏 可以存放资料) 非静态类(实例化)
构造函数(必须是public)
构造方法与实例方法的区别:
构造方法:
- 用于对象的初始化,一个类至少有一个构造方法
- 不能显示调用,只能在创建对象的时候调用,使用呢我关键字
- 构造方法不能有返回值
- 构造方法的名称必须和类名一样
实例方法:
- 用于表示对象能干什么,一个类可以没有任何的实例方法
- 只能显示调用:类名.方法名
- 可以有返回值,没有时必须一void表示
- 方法的命名要有意义,一般是“动词+名词”形式,不能与类名相同,命名规范采用Pascal命名法
this关键字:
1.当成员变量和局部变量重名时使用this区分(不带this一遍的参数为局部变量)
下面的构造方法可以调用上面的构造方法
2.this表示当前类的对象,用于访问该类成员变量或方法
析构函数(~)(当程序结束时才执行,帮助我们释放资源)
命名空间
引用--》右键--》添加引用
值类型和引用类型
字符串的不可变性(栈地址不变 改变的是指向)
string s1 = “张三”;
s1= “孙权”;
字符串的方法
string类型只可读
string s = "ancdedg"; Console.WriteLine(s[0]); Console.ReadKey(); ------------------------------------------------ string s = "ancdedg"; s[0] = b; .//会报错不能进行修改 Console.ReadKey(); ------------------------------------------------ 把字符串类型转换为 cahr类型先进行修改 //s.toCharArray() string s = "abcdefg"; //将字符串转换成字符数组 char[] chs = s.ToCharArray(); chs[0] = 'b'; //将字符数组转换成字符串 Console.WriteLine(s[0]); s = new string(chs); Console.ReadKey();
StringBuilder (不开空间)
StringBuilder sb = new StringBuilder(); string str = null; Stopwatch sw = new Stopwatch();//shift+alt+f10 //sw.Start(); //for (int i = 0; i < 100000; i++) //{ // str += i; //} //sw.Stop(); //Console.WriteLine(sw.Elapsed); sw.Start(); for (int i = 0; i < 100000; i++) { sb.Append(i); } sw.Stop(); Console.WriteLine(sb.ToString()); Console.WriteLine(sw.Elapsed); Console.ReadKey();
toupper,tolower 可以包含数字
选removeemptyenyries
C# 字符串(String)
在 C# 中,您可以使用字符数组来表示字符串,但是,更常见的做法是使用 string 关键字来声明一个字符串变量。string 关键字是 System.String 类的别名。
创建 String 对象
您可以使用以下方法之一来创建 string 对象:
- 通过给 String 变量指定一个字符串
- 通过使用 String 类构造函数
- 通过使用字符串串联运算符( + )
- 通过检索属性或调用一个返回字符串的方法
- 通过格式化方法来转换一个值或对象为它的字符串表示形式
下面的实例演示了这点:
实例
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
//字符串,字符串连接
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
//通过使用 string 构造函数
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//用于转化值的格式化方法
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}",
waiting);
Console.WriteLine("Message: {0}", chat);
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
FullName:RowanAtkinsonGreetings:HelloMessage:HelloFromTutorialsPointMessage:Message sent at 17:58 on Wednesday,10October2012
String 类的属性
String 类有以下两个属性:
序号 | 属性名称 & 描述 |
---|---|
1 | Chars 在当前 String 对象中获取 Char 对象的指定位置。 |
2 | Length 在当前的 String 对象中获取字符数。 |
String 类的方法
String 类有许多方法用于 string 对象的操作。下面的表格提供了一些最常用的方法:
序号 | 方法名称 & 描述 |
---|---|
1 | public static int Compare( string strA, string strB ) 比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。该方法区分大小写。 |
2 | public static int Compare( string strA, string strB, bool ignoreCase ) 比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。但是,如果布尔参数为真时,该方法不区分大小写。 |
3 | public static string Concat( string str0, string str1 ) 连接两个 string 对象。 |
4 | public static string Concat( string str0, string str1, string str2 ) 连接三个 string 对象。 |
5 | public static string Concat( string str0, string str1, string str2, string str3 ) 连接四个 string 对象。 |
6 | public bool Contains( string value ) 返回一个表示指定 string 对象是否出现在字符串中的值。 |
7 | public static string Copy( string str ) 创建一个与指定字符串具有相同值的新的 String 对象。 |
8 | public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count ) 从 string 对象的指定位置开始复制指定数量的字符到 Unicode 字符数组中的指定位置。 |
9 | public bool EndsWith( string value ) 判断 string 对象的结尾是否匹配指定的字符串。 |
10 | public bool Equals( string value ) 判断当前的 string 对象是否与指定的 string 对象具有相同的值。 |
11 | public static bool Equals( string a, string b ) 判断两个指定的 string 对象是否具有相同的值。 |
12 | public static string Format( string format, Object arg0 ) 把指定字符串中一个或多个格式项替换为指定对象的字符串表示形式。 |
13 | public int IndexOf( char value ) 返回指定 Unicode 字符在当前字符串中第一次出现的索引,索引从 0 开始。 |
14 | public int IndexOf( string value ) 返回指定字符串在该实例中第一次出现的索引,索引从 0 开始。 |
15 | public int IndexOf( char value, int startIndex ) 返回指定 Unicode 字符从该字符串中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。 |
16 | public int IndexOf( string value, int startIndex ) 返回指定字符串从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。 |
17 | public int IndexOfAny( char[] anyOf ) 返回某一个指定的 Unicode 字符数组中任意字符在该实例中第一次出现的索引,索引从 0 开始。 |
18 | public int IndexOfAny( char[] anyOf, int startIndex ) 返回某一个指定的 Unicode 字符数组中任意字符从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。 |
19 | public string Insert( int startIndex, string value ) 返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。 |
20 | public static bool IsNullOrEmpty( string value ) 指示指定的字符串是否为 null 或者是否为一个空的字符串。 |
21 | public static string Join( string separator, string[] value ) 连接一个字符串数组中的所有元素,使用指定的分隔符分隔每个元素。 |
22 | public static string Join( string separator, string[] value, int startIndex, int count ) 连接接一个字符串数组中的指定位置开始的指定元素,使用指定的分隔符分隔每个元素。 |
23 | public int LastIndexOf( char value ) 返回指定 Unicode 字符在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。 |
24 | public int LastIndexOf( string value ) 返回指定字符串在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。 |
25 | public string Remove( int startIndex ) 移除当前实例中的所有字符,从指定位置开始,一直到最后一个位置为止,并返回字符串。 |
26 | public string Remove( int startIndex, int count ) 从当前字符串的指定位置开始移除指定数量的字符,并返回字符串。 |
27 | public string Replace( char oldChar, char newChar ) 把当前 string 对象中,所有指定的 Unicode 字符替换为另一个指定的 Unicode 字符,并返回新的字符串。 |
28 | public string Replace( string oldValue, string newValue ) 把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。 |
29 | public string[] Split( params char[] separator ) 返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。 |
30 | public string[] Split( char[] separator, int count ) 返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。int 参数指定要返回的子字符串的最大数目。 |
31 | public bool StartsWith( string value ) 判断字符串实例的开头是否匹配指定的字符串。 |
32 | public char[] ToCharArray() 返回一个带有当前 string 对象中所有字符的 Unicode 字符数组。 |
33 | public char[] ToCharArray( int startIndex, int length ) 返回一个带有当前 string 对象中所有字符的 Unicode 字符数组,从指定的索引开始,直到指定的长度为止。 |
34 | public string ToLower() 把字符串转换为小写并返回。 |
35 | public string ToUpper() 把字符串转换为大写并返回。 |
36 | public string Trim() 移除当前 String 对象中的所有前导空白字符和后置空白字符。 |
上面的方法列表并不详尽,请访问 MSDN 库,查看完整的方法列表和 String 类构造函数。
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str1 = "This is test"; string str2 = "This is text"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal."); } else { Console.WriteLine(str1 + " and " + str2 + " are not equal."); } Console.ReadKey() ; } } } 当上面的代码被编译和执行时,它会产生下列结果: This is test and This is text are not equal. 字符串包含字符串: 实例 using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str = "This is test"; if (str.Contains("test")) { Console.WriteLine("The sequence 'test' was found."); } Console.ReadKey() ; } } } 当上面的代码被编译和执行时,它会产生下列结果: The sequence 'test' was found. 获取子字符串: 实例 using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str = "Last night I dreamt of San Pedro"; Console.WriteLine(str); string substr = str.Substring(23);
//截取字符串包含指定索引 str.Substring(23,30);、、指定截取范围
Console.WriteLine(substr); Console.ReadKey() ; } } } 运行实例 » 当上面的代码被编译和执行时,它会产生下列结果: Last night I dreamt of San Pedro San Pedro 连接字符串: 实例 using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string[] starray = new string[]{"Down the way nights are dark", "And the sun shines daily on the mountain top", "I took a trip on a sailing ship", "And when I reached Jamaica", "I made a stop"}; string str = String.Join("\n", starray); Console.WriteLine(str); Console.ReadKey() ; } } } 当上面的代码被编译和执行时,它会产生下列结果: Down the way nights are dark And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop C# 数组(Array)C# 结构体(Struct) 1 篇笔记 写笔记 victoryckl 137***570@qq.com 参考地址 83 C# string.Format格式化日期 DateTime dt = new DateTime(2017,4,1,13,16,32,108); string.Format("{0:y yy yyy yyyy}",dt); //17 17 2017 2017 string.Format("{0:M MM MMM MMMM}", dt);//4 04 四月 四月 string.Format("{0:d dd ddd dddd}", dt);//1 01 周六 星期六 string.Format("{0:t tt}", dt);//下 下午 string.Format("{0:H HH}", dt);//13 13 string.Format("{0:h hh}", dt);//1 01 string.Format("{0:m mm}", dt);//16 16 string.Format("{0:s ss}", dt);//32 32 string.Format("{0:F FF FFF FFFF FFFFF FFFFFF FFFFFFF}", dt);//1 1 108 108 108 108 108 string.Format("{0:f ff fff ffff fffff ffffff fffffff}", dt);//1 10 108 1080 10800 108000 1080000 string.Format("{0:z zz zzz}", dt);//+8 +08 +08:00 string.Format("{0:yyyy/MM/dd HH:mm:ss.fff}",dt); //2017/04/01 13:16:32.108 string.Format("{0:yyyy/MM/dd dddd}", dt); //2017/04/01 星期六 string.Format("{0:yyyy/MM/dd dddd tt hh:mm}", dt); //2017/04/01 星期六 下午 01:16 string.Format("{0:yyyyMMdd}", dt); //20170401 string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", dt); //2017-04-01 13:16:32.108 除去string.Format()可以对日期进行格式化之外,*.ToString()也可以实现相同的效果: DateTime dt = new DateTime(2017,4,1,13,16,32,108); dt.ToString("y yy yyy yyyy");//17 17 2017 2017 dt.ToString("M MM MMM MMMM");//4 04 四月 四月 dt.ToString("d dd ddd dddd");//1 01 周六 星期六 dt.ToString("t tt");//下 下午 dt.ToString("H HH");//13 13 dt.ToString("h hh");//1 01 dt.ToString("m mm");//16 16 dt.ToString("s ss");//32 32 dt.ToString("F FF FFF FFFF FFFFF FFFFFF FFFFFFF");//1 1 108 108 108 108 108 dt.ToString("f ff fff ffff fffff ffffff fffffff");//1 10 108 1080 10800 108000 1080000 dt.ToString("z zz zzz");//+8 +08 +08:00 dt.ToString("yyyy/MM/dd HH:mm:ss.fff"); //2017/04/01 13:16:32.108 dt.ToString("yyyy/MM/dd dddd"); //2017/04/01 星期六 dt.ToString("yyyy/MM/dd dddd tt hh:mm"); //2017/04/01 星期六 下午 01:16 dt.ToString("yyyyMMdd"); //20170401 dt.ToString("yyyy-MM-dd HH:mm:ss.fff"); //2017-04-01 13:16:32.108
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str1 = "This is test";
string str2 = "This is text";
if (String.Compare(str1, str2) == 0)
{
Console.WriteLine(str1 + " and " + str2 + " are equal.");
}
else
{
Console.WriteLine(str1 + " and " + str2 + " are not equal.");
}
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Thisis test andThisis text are not equal.
字符串包含字符串:
实例
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str = "This is test";
if (str.Contains("test"))
{
Console.WriteLine("The sequence 'test' was found.");
}
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
The sequence 'test' was found.
获取子字符串:
实例
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string str = "Last night I dreamt of San Pedro";
Console.WriteLine(str);
string substr = str.Substring(23);
Console.WriteLine(substr);
Console.ReadKey() ;
}
}
}
运行实例 »
当上面的代码被编译和执行时,它会产生下列结果:
Last night I dreamt of SanPedroSanPedro
连接字符串:
实例
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str = String.Join("\n", starray);
Console.WriteLine(str);
Console.ReadKey() ;
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
Andwhen I reached Jamaica
I made a stop
1 篇笔记 写笔记
继承(冒号)
访问修饰符符> class <基类>
{
...
}
class <派生类> : <基类>
{
...
}
using System; namespace InheritanceApplication { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // 派生类 class Rectangle: Shape { public int getArea() { return (width * height); } } class RectangleTester { static void Main(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // 打印对象的面积 Console.WriteLine("总面积: {0}", Rect.getArea()); Console.ReadKey(); } } }
---------------------------------------------------------------------------
基类的初始化
派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中进行父类的初始化。
下面的程序演示了这点:
实例
namespace RectangleApplication
{
class Rectangle
{
// 成员变量
protected double length;
protected double width;
public Rectangle(double l, double w) //有参初始化
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("长度: {0}", length);
Console.WriteLine("宽度: {0}", width);
Console.WriteLine("面积: {0}", GetArea());
}
}//end class Rectangle
class Tabletop : Rectangle
{
private double cost;
public Tabletop(double l, double w) : base(l, w) //调用父类代餐初始化
{ }
public double GetCost()
{
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display()
{
base.Display();
Console.WriteLine("成本: {0}", GetCost());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
![]()
//ID 为子类特有的
-----------------------------------------------------------------------------------
多继承
class Rectangle : Shape, PaintCost
关键字new隐藏父类成员
子类函数加 new 可以隐藏从父类继承的同名成员
里氏转换
is 和 as
protect
ArrayList(集合) 和 HashTable :collection
ArrayList 相当于 python中的 list 可以添加任意类型数据 取出时是object类型
ArrayList 类的方法和属性
下表列出了 ArrayList 类的一些常用的 属性:
属性 | 描述 |
---|---|
Capacity | 获取或设置 ArrayList 可以包含的元素个数。 //集合长度可变 |
Count | 获取 ArrayList 中实际包含的元素个数。 |
IsFixedSize | 获取一个值,表示 ArrayList 是否具有固定大小。 |
IsReadOnly | 获取一个值,表示 ArrayList 是否只读。 |
IsSynchronized | 获取一个值,表示访问 ArrayList 是否同步(线程安全)。 |
Item[Int32] | 获取或设置指定索引处的元素。 |
SyncRoot | 获取一个对象用于同步访问 ArrayList。 |
下表列出了 ArrayList 类的一些常用的 方法:
序号 | 方法名 & 描述 |
---|---|
1 | public virtual int Add( object value ); 在 ArrayList 的末尾添加一个对象。 |
2 | public virtual void AddRange( ICollection c ); 在 ArrayList 的末尾添加 ICollection 的元素。 |
3 | public virtual void Clear(); 从 ArrayList 中移除所有的元素。 |
4 | public virtual bool Contains( object item ); 判断某个元素是否在 ArrayList 中。 |
5 | public virtual ArrayList GetRange( int index, int count ); 返回一个 ArrayList,表示源 ArrayList 中元素的子集。 |
6 | public virtual int IndexOf(object); 返回某个值在 ArrayList 中第一次出现的索引,索引从零开始。 |
7 | public virtual void Insert( int index, object value ); 在 ArrayList 的指定索引处,插入一个元素。 |
8 | public virtual void InsertRange( int index, ICollection c ); 在 ArrayList 的指定索引处,插入某个集合的元素。 |
9 | public virtual void Remove( object obj ); 从 ArrayList 中移除第一次出现的指定对象。 |
10 | public virtual void RemoveAt( int index ); 移除 ArrayList 的指定索引处的元素。 |
11 | public virtual void RemoveRange( int index, int count ); 从 ArrayList 中移除某个范围的元素。 |
12 | public virtual void Reverse(); 逆转 ArrayList 中元素的顺序。 |
13 | public virtual void SetRange( int index, ICollection c ); 复制某个集合的元素到 ArrayList 中某个范围的元素上。 |
14 | public virtual void Sort(); 对 ArrayList 中的元素进行排序。 |
15 | public virtual void TrimToSize(); 设置容量为 ArrayList 中元素的实际个数。 |
实例
下面的实例演示了动态数组(ArrayList)的概念:
实例
using System.Collections;
namespace CollectionApplication
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
Console.WriteLine("Adding some numbers:");
al.Add(45);
al.Add(78);
al.Add(33);
al.Add(56);
al.Add(12);
al.Add(23);
al.Add(9);
Console.WriteLine("Capacity: {0} ", al.Capacity);
Console.WriteLine("Count: {0}", al.Count);
Console.Write("Content: ");
foreach (int i in al)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.Write("Sorted Content: ");
al.Sort();
foreach (int i in al)
{
Console.Write(i + " ");
}
Console.WriteLine();
Console.ReadKey();
}
}
}
remove("内容")
HashTable
相当于python dict
Hashtable 类代表了一系列基于键的哈希代码组织起来的键/值对。它使用键来访问集合中的元素。
当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个键/值对。键用于访问集合中的项目。
Hashtable 类的方法和属性
下表列出了 Hashtable 类的一些常用的 属性:
属性 | 描述 |
---|---|
Count | 获取 Hashtable 中包含的键值对个数。 |
IsFixedSize | 获取一个值,表示 Hashtable 是否具有固定大小。 |
IsReadOnly | 获取一个值,表示 Hashtable 是否只读。 |
Item | 获取或设置与指定的键相关的值。 |
Keys | 获取一个 ICollection,包含 Hashtable 中的键。 |
Values | 获取一个 ICollection,包含 Hashtable 中的值。 |
下表列出了 Hashtable 类的一些常用的 方法:
序号 | 方法名 & 描述 |
---|---|
1 | public virtual void Add( object key, object value ); 向 Hashtable 添加一个带有指定的键和值的元素。 |
2 | public virtual void Clear(); 从 Hashtable 中移除所有的元素。 |
3 | public virtual bool ContainsKey( object key ); 判断 Hashtable 是否包含指定的键。 |
4 | public virtual bool ContainsValue( object value ); 判断 Hashtable 是否包含指定的值。 |
5 | public virtual void Remove( object key ); 从 Hashtable 中移除带有指定的键的元素。 |
实例
下面的实例演示了哈希表(Hashtable)的概念:
实例
using System.Collections;
namespace CollectionsApplication
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("001", "Zara Ali");
ht.Add("002", "Abida Rehman");
ht.Add("003", "Joe Holzner");
ht.Add("004", "Mausam Benazir Nur");
ht.Add("005", "M. Amlan");
ht.Add("006", "M. Arif");
ht.Add("007", "Ritesh Saikia");
if (ht.ContainsValue("Nuha Ali"))
{
Console.WriteLine("This student name is already in the list");
}
else
{
ht.Add("008", "Nuha Ali");
}
// 获取键的集合
ICollection key = ht.Keys;
foreach (string k in key)
{
Console.WriteLine(k + ": " + ht[k]);
}
Console.ReadKey();
}
}
}
var 可以使任何类型
更改
泛型集合list
装箱和拆箱( ---》object)
值类型==数值类型
string 引用类型 int 值类型(数值类型)
string---》int 没有继承关系不是拆箱
字典集合
path类
文件类(File)
简单的文件读写
File 和 Path 为静态类 使用方法 类名.方法
File
writeallbytes readallbytes 以字节写读
string st = "hello 今天天气好晴朗,处处好风光"; byte[] buffer = Encoding.UTF8.GetBytes(st); File.WriteAllBytes(@"D:\practice\c#\practice\faceprojiect\day3_demo\demotext", buffer); byte[] buffer1 = File.ReadAllBytes(@"D:\practice\c#\practice\faceprojiect\day3_demo\demotext"); string s = Encoding.UTF8.GetString(buffer1); Console.WriteLine(s); Console.ReadKey(); //gbk 需要getencoding
File.ReadAllText 读取整个文件 返回字符串 string
File.ReadAllLines 一行一行的读取 返回数组string [ ]
写数据
//3种 均覆盖重写
WriteLine
WriteAllLines 以数组形式写入
WriteAllText 以字符串形式写入
追加
File.AppendAllText
文件流
FileStream //操作字节
StreamReader 和 StreamWriter //操作字符
参数 描述 FileMode //对文件操作方法 FileMode 枚举定义了各种打开文件的方法。FileMode 枚举的成员有: Append:打开一个已有的文件,并将光标放置在文件的末尾。如果文件不存在,则创建文件。 Create:创建一个新的文件。如果文件已存在,则删除旧文件,然后创建新文件。 CreateNew:指定操作系统应创建一个新的文件。如果文件已存在,则抛出异常。 Open:打开一个已有的文件。如果文件不存在,则抛出异常。 OpenOrCreate:指定操作系统应打开一个已有的文件。如果文件不存在,则用指定的名称创建一个新的文件打开。 Truncate:打开一个已有的文件,文件一旦打开,就将被截断为零字节大小。然后我们可以向文件写入全新的数据,但是保留文件的初始创建日期。如果文件不存在,则抛出异常。
FileAccess //对数据操作方法 FileAccess 枚举的成员有:Read、ReadWrite 和 Write。
FileShare FileShare 枚举的成员有: Inheritable:允许文件句柄可由子进程继承。Win32 不直接支持此功能。 None:谢绝共享当前文件。文件关闭前,打开该文件的任何请求(由此进程或另一进程发出的请求)都将失败。 Read:允许随后打开文件读取。如果未指定此标志,则文件关闭前,任何打开该文件以进行读取的请求(由此进程或另一进程发出的请求)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。 ReadWrite:允许随后打开文件读取或写入。如果未指定此标志,则文件关闭前,任何打开该文件以进行读取或写入的请求(由此进程或另一进程发出)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。 Write:允许随后打开文件写入。如果未指定此标志,则文件关闭前,任何打开该文件以进行写入的请求(由此进程或另一进过程发出的请求)都将失败。但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。 Delete:允许随后删除文件。 实例 下面的程序演示了 FileStream 类的用法: 实例 using System; using System.IO; namespace FileIOApplication { class Program { static void Main(string[] args) { FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite); for (int i = 1; i <= 20; i++) { F.WriteByte((byte)i); } F.Position = 0; for (int i = 0; i <= 20; i++) { Console.Write(F.ReadByte() + " "); } F.Close(); Console.ReadKey(); } } } 当上面的代码被编译和执行时,它会产生下列结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1
filestream
多余容量使用空格
没有多余空格
读取
写入
复制
filestreamreader filestreamwriter(没有FileMode和FileAccess)
写入文本文件
true 是追加
多态
虚方法 virtual 声明 已知方法里的方法体
抽象类 方法里没有方法体 没有{ }
抽象类
非抽象类里不能写抽象类成员
抽象类里可以写费抽象类成员 虽然非抽象类成员在该类里不能使用但是其子类可以使用
模拟u盘
main函数
修饰符 工厂设计模式 值传递和引用 序列化和反序列化 部分类 密封类
重写父类的tostring() 接口 显示接口
修饰符
protected 可以跨项目
internal 不可以跨项目
工厂模式 : 父类替代子类问题
值类型和引用类型 值在栈中 引用在堆中
值类型赋值 会开辟新的栈空间 int n1 = 10 ; int n2 =n1; 当改变其中一个内容时 二者不会一起改变
加ref 会把地址赋值给另一个值 当改变其中一个内容时 二者会一起改变
引用类型赋值 不会开辟新的堆地址 当改变其摘的内容时 二者均会改变
序列化和反序列化
部分类(可以写重名类)实质是一个类 可以用另一个里的私有属性和方法
密封类 不能够被继承 但是能继承别人
重写tostring方法 (重载方法)
victoryckl
137***570@qq.com
参考地址
C# string.Format格式化日期
DateTime dt =newDateTime(2017,4,1,13,16,32,108);string.Format("{0:y yy yyy yyyy}",dt);//17 17 2017 2017string.Format("{0:M MM MMM MMMM}", dt);
//4 04 四月 四月string.Format("{0:d dd ddd dddd}", dt);//1 01 周六 星期六string.Format("{0:t tt}", dt);//下 下午string.Format("{0:H HH}", dt);
//13 13string.Format("{0:h hh}", dt);//1 01string.Format("{0:m mm}", dt);
//16 16string.Format("{0:s ss}", dt);//32 32string.Format("{0:F FF FFF FFFF FFFFF FFFFFF FFFFFFF}", dt);
//1 1 108 108 108 108 108string.Format("{0:f ff fff ffff fffff ffffff fffffff}", dt);
//1 10 108 1080 10800 108000 1080000string.Format("{0:z zz zzz}", dt);//+8 +08 +08:00string.Format("{0:yyyy/MM/dd HH:mm:ss.fff}",dt);
//2017/04/01 13:16:32.108string.Format("{0:yyyy/MM/dd dddd}", dt);
//2017/04/01 星期六string.Format("{0:yyyy/MM/dd dddd tt hh:mm}", dt);//2017/04/01 星期六 下午 01:16string.Format("{0:yyyyMMdd}", dt);
//20170401string.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", dt); //2017-04-01 13:16:32.108
除去string.Format()可以对日期进行格式化之外,*.ToString()也可以实现相同的效果:
DateTime dt =newDateTime(2017,4,1,13,16,32,108); dt.ToString("y yy yyy yyyy");//17 17 2017 2017 dt.ToString("M MM MMM MMMM");//4 04 四月 四月 dt.ToString("d dd ddd dddd");//1 01 周六 星期六 dt.ToString("t tt");//下 下午 dt.ToString("H HH");//13 13 dt.ToString("h hh");//1 01 dt.ToString("m mm");//16 16 dt.ToString("s ss");//32 32 dt.ToString("F FF FFF FFFF FFFFF FFFFFF FFFFFFF");//1 1 108 108 108 108 108 dt.ToString("f ff fff ffff fffff ffffff fffffff");//1 10 108 1080 10800 108000 1080000 dt.ToString("z zz zzz");//+8 +08 +08:00 dt.ToString("yyyy/MM/dd HH:mm:ss.fff"); //2017/04/01 13:16:32.108 dt.ToString("yyyy/MM/dd dddd"); //2017/04/01 星期六 dt.ToString("yyyy/MM/dd dddd tt hh:mm");//2017/04/01 星期六 下午 01:16 dt.ToString("yyyyMMdd"); //20170401 dt.ToString("yyyy-MM-dd HH:mm:ss.fff"); //2017-04-01 13:16:32.108