1. 显式类型转换三种方式:
1) (类型名)表达式
int a = (int)10.2;
2) Convert.To类型名(表达式)
double a = 10;
int b = Convert.ToInt32(a);
3) 类型名.Parse(表达式)
double a = 10;
int b = Int32.Parse(a.ToString());
2. 类型转换——装箱和拆箱
-
short -> System.Int16
-
int -> System.Int32
-
long -> System.Int64
3. 常量
1) 静态常量 const
const double PI = 3.14;
2) 动态常量 readonly
4. 字符串操作
1) 格式化日期数据
DateTime dt = DateTime.Now;
string strDT = String.Format("{0:D}", dt); // 2019年12月27日
2) 根据索引截取字符串
string Substring(int startIndex, int length): 第二个参数是截取长度
string s = "abc def";
string ss = s.Substring(0, 3); // abc
3) 根据指定的分隔符分割字符串
string str = "a,b,c";
string[] strs = str.Split(new char[]{','});
string[] strs = str.Split(','); // 这两种方式效果一样
4) 插入字符串
string Insert(int startIndex, string value): 插入的起始位置,插入的字符串
string name = "abc";
string email = name.Insert(name.Length, "@qq.com"); // abc@qq.com
5) 填充字符串
string PadLeft(int totalWidth, char paddingChar)
string PadRight(int totalWidth, char paddingChar): 填充后的字符串长度,要填充的字符串
string s = "abc";
string ss = s.PadLeft(s.Length+3, '('); // (((abc
6) 删除字符串
string remove(int startIndex, int count): 开始删除的起始位置,要删除字符的数量
string name = "zhangsan";
string s = name.Remove(5, 3); // zhang
7) 复制字符串
Copy(string str): 全部复制
CopyTo(int Index, char[] destination, int destinationIndex, int count): 部分复制
8) 替换字符串
string Replace(char OChar, char NChar)
string Replace(string OValue, string NValue)
5. 二维数组
初始化:int[,] arr = new int[,]{{1,2}, {3,4}};
获取二维数组的维数:arr.Rank
获取行数:arr.GetLength(0)
int[,] arr = new int[3,3]{{1,2,3},{4,5,6},{7,8,9}};
Console.WriteLine(arr.Rank); // 2
Console.WriteLine(arr.GetLength(0)); // 3
6. HashTable
1) 循环输出HashTable集合的元素
DictionaryEntry:用来定义可设置或检索的字典键/值对
Hashtable ht = new Hashtable();
ht.Add("id", "001");
ht.Add("name", "zhangsan");
foreach (DictionaryEntry dicEntry in ht)
{
Console.WriteLine(dicEntry.Key + ": " + dicEntry.Value);
// id: 001
// name: zhangsan
}
2) 元素的查找
Contains、ContainsKey方法——按键查找
Hashtable ht = new Hashtable();
ht.Add("id", "001");
ht.Add("name", "zhangsan");
Console.WriteLine(ht.ContainsKey("id")); // True
ContainsValue方法——按值查找
Hashtable ht = new Hashtable();
ht.Add("id", "001");
ht.Add("name", "zhangsan");
Console.WriteLine(ht.ContainsValue("001")); // True
7. 引用参数
public class SampleClass3 //声明类SampleClass3
{
int num = 10; //私有字段
public void method(ref SampleClass3 s1) //定义公共方法method
{
s1.num=20;
s1=new SampleClass3();
s1.num=30;
}
public void dispnum() //输出num
{
Console.WriteLine(num);
}
}
static void Main(string[] args)
{
SampleClass3 s = new SampleClass3(); //创建对象s
s.method(ref s); //调用method方法
s.dispnum(); //输出:30
}
8. 继承和接口设计
1) 继承
class A
{
private int n = 10;
public void afun()
{
Console.WriteLine(n);
}
}
class B : A
{
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.afun(); // 10
}
}
可以使用sealed修饰符来禁止继承
例:sealed class 类名 {}
2) 隐藏基类方法
方法1:使用新的派生成员替换基成员,称为隐藏
class A
{
private int n = 10;
public void fun()
{
Console.WriteLine("aaa");
}
}
class B : A
{
new public void fun() // 隐藏基类方法fun
{
Console.WriteLine("bbb");
}
}
class Program
{
static void Main(string[] args)
{
B b = new B();
b.fun(); // bbb
}
}
方法2:重写虚拟的基成员
- virtual关键字
virtual关键字用于修饰方法、属性、索引器或事件声明,称为虚拟方法,并且允许在派生类中重写这些对象。 - override关键字
override方法提供从基类继承的成员的新实现。通过override声明重写的方法称为重写基方法。
注意:不能重写非虚方法或静态方法。重写的基方法必须是virtual、abstract或override的。
3) 例题
设计一个控制台应用程序,采用虚方法求长方形、圆、圆球体和圆柱体的面积或表面积
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestDemo
{
public class Rectangle // 长方形类
{
public const double PI = Math.PI;
protected double x, y;
public Rectangle()
{
}
public Rectangle(double x, double y)
{
this.x = x;
this.y = y;
}
public virtual double Area()
{
return x * y;
}
}
public class Circle : Rectangle // 圆类
{
public Circle(double r) : base(r, 0) { }
public override double Area()
{
return PI * x * x;
}
}
public class Sphere : Rectangle // 圆球体类
{
public Sphere(double r) : base(r, 0) { }
public override double Area()
{
return 4 * PI * x * x;
}
}
public class Cylinder : Rectangle // 圆柱体类
{
public Cylinder(double r, double h) : base(r, h) { }
public override double Area()
{
return 2 * PI * x * x + 2 * PI * x * y;
}
}
class program
{
static void Main(string[] args)
{
double x = 2.4;
double y = 5.6;
double r = 3.0;
double h = 5.0;
Rectangle t = new Rectangle(x, y);
Rectangle c = new Circle(r);
Rectangle s = new Sphere(r);
Rectangle l = new Cylinder(r, h);
Console.WriteLine("长为{0},宽为{1}的长方形面积={2:F2}", x, y, t.Area());
Console.WriteLine("半径为{0}的圆面积={1:F2}", r, c.Area());
Console.WriteLine("半径为{0}的圆球体表面积={1:F2}", r, s.Area());
Console.WriteLine("半径为{0},高度为{1}的圆柱体表面积={2:F2}", r, h, l.Area());
}
}
}
4) is运算符
语法格式:数据 is type
A a = new A();
if (a is A)
{
Console.WriteLine("a是A类型");
}
else
{
Console.WriteLine("a不是A类型");
}
5) as运算符
语法格式:operand as type 等效于 operand is type ? (type)operand : (type)null
as运算符仅适合以下情况:
• operand的类型是type类型
• operand可以隐式转换为type类型
• operand可以装箱到type中