目录
[枚举 关键字 enum](#枚举 关键字 enum)
[编辑 显式转换](#编辑 显式转换)
[编辑 字符串的使用](#编辑 字符串的使用)
[编辑 截取字符串](#编辑 截取字符串)
[编辑 arraylist](#编辑 arraylist)
整数

浮点


decimal


bool


引用

object
object o1 = 4;
object o2 = "123";


string


值
左值,右引用

值不影响别的参数,引用影响
stamp stamp1 = new stamp { name = "1", age = 25 };
stamp stamp2 = new stamp { name = "2", age = 30 };
int age = stamp1.age;
stamp1.age = 35;
stamp stamp3 = stamp2;
stamp2.name = "3";
Console.WriteLine("stamp1 age: "+ stamp1.age);
Console.WriteLine("age:"+age);
Console.WriteLine("stamp2 age: " + stamp2.name);
Console.WriteLine("stamp3 age: " + stamp3.name);

枚举 关键字 enum

类型转换

隐式转换

显式转换

装箱,拆箱
二者转换过程中必须类型相同


常量

readonly要定义在类中,可以直接赋值,也可以在构造方法中赋值,const是编译常量在编译时值就已经确定,而readonly是运行时常量,运行时才确定值。


static void Main(string[] args) //程序入口
{
const double PI = 3.14;
double r;
Console.WriteLine("shuru");
r = Convert.ToDouble(Console.ReadLine());
//因为readline返回的是字符串,所以要进行转化
Console.WriteLine(PI*r);
}
表达式
使用%运算符时,结果和%左边的符号相同



字符串的使用


比较字符串

字符格式化





截取字符串


字符串的分割


插入字符串


删除字符串


字符串的复制



替换字符串


可变字符串类








switch


do...while

foreach
转移语句








数组




arraylist




cs
1) ArrayList list1 = new ArrayList();
2) int[] arr = new int[]{} ;
ArrayList list2 = new ArrayList(arr) ;
3) ArrayList list3 = new ArrayList(10) ;
ArrayList的方法
1)public virtual int Add(Object value)
-
public virtual void Insert(int index, Object value)
-
InsertRange(int Index, 一维数组的名称)
-
public virtual void Clear() --移除所有元素
5)public virtual void Remove(Object obhj) ---删除指定的元素
6)public virtual void RemoveAt(int Index) ---删除指定索引的元素
7)public virtual void RemoveRange(int index, int count)--删除指定的多个元素
8) foreach遍历ArrayList
9) IndexOf("A") ---返回A第一次出现的索引的位置,未找到的话返回-1;
10) IndexOf("A", 7) ---从索引7开始查找,未找到返回-1 ;
11) LastIndexOf("A") ---最后一次出现A的索引
12)LastIndexOf("A", 7)---下标0~7这段最后一次出现A的位置索引
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_34177692/article/details/141942547
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//ArrayList类位于System.Collections命名空间内
using System.Collections;
//ArrayList是一种动态数组
//ArrayList的容量可以根据需要自动扩充
//ArrayList提供了添加,删除,和插入某一范围元素的方法
//Array List提供了将只读和固定大小包装返回集合的方法,数组不提供
//Array List只能是一维的形式,数组可以多维
namespace ArrayListDemo01
{
internal class Program
{
static void Main(string[] args)
{
//ArrayList的构造函数
//以默认(16位)大小来初始化内部的数组
ArrayList list = new ArrayList();
//用一个ICollection对象来构造,并把集合的元素添加到ArrayList中
int[] arr1 = {10,9,8,7,6,5,4,3,2,1,0,11,12,13,14,15,16};
ArrayList list1 = new ArrayList(arr1 );
//用指定大小初始化内部数组
ArrayList list2 = new ArrayList(20);
Console.WriteLine("list中包含的元素数为:");
//ArrayList的属性Capacity获取或设置ArrayList中可包含的的元素数
Console.WriteLine(list.Capacity);
list.Capacity = 10;
Console.WriteLine("list可以包含的元素数:");
Console.WriteLine(list.Capacity);
//ArrayList中的属性Count是获取实际包含的元素数
//如果ArrayList没有被初始化,那么count=0;
Console.WriteLine("list中实际包含的元素数:");
Console.WriteLine(list.Count);
//ArrayList的初始化
for(int i=0; i<list.Capacity; i++)
{
list.Add(i);
}
Console.WriteLine("初始化后,list内包含的元素数:");
Console.WriteLine(list.Count);
//常用的属性及说明
//IsFixedSize
Console.WriteLine(list1.IsFixedSize); //false
//IsReadOnly
Console.WriteLine(list1.IsReadOnly); //false
//Item 获取或设置指定索引处的元素
//和数组访问元素的方法一样
Console.WriteLine(list1[5]);
//ArrayList中的方法
//public virtual int Add(Object value)
//virtual方法在派生类中可以不重写,有方法体
//抽象方法只有声明没有方法体,派生类中必须重写
list1.Add(17);
list1.Add(18);
foreach(int i in list1)
{
Console.Write(i + " ");
}
Console.WriteLine();
//Insert(), 插入元素
//public virtual void Insert(int index, Object value)
list1.Insert(0, 100);
foreach (int i in list1)
{
Console.Write(i + " ");
}
Console.WriteLine();
//ArrayList插入一个数组
//InsertRange()
int[] arr2 = {11,22,33,44,55,66,77 };
list1.InsertRange(5,arr2);
foreach (int i in list1)
{
Console.Write(i + " ");
}
Console.WriteLine();
//ArrayList删除一个元素
int[] array = {11,2,3,44,55,55,55,66,77,88,99,100,101,102 };
ArrayList al1 = new ArrayList(array);
//Remove()从ArrayList中移除特定对象的第一个匹配项
al1.Remove(55);//括号里面是ArrayList中的元素
foreach (int i in al1)
{
Console.Write(i + " ");
}
Console.WriteLine();
//RemoveAt()移除指定索引的元素
al1.RemoveAt(5);//括号里面是索引
foreach (int i in al1)
{
Console.Write(i + " ");
}
Console.WriteLine();
//RemoveRange()
//public virtual void RemoveRange(int index, int count)
al1.RemoveRange(4, 3);
foreach (int i in al1)
{
Console.Write(i + " ");
}
Console.WriteLine();
//clear()删除所有元素
al1.Clear();
Console.WriteLine(al1.Count);//获取实际包含的元素数
Console.WriteLine(al1.Capacity);//获取或设置可包含的元素数
Console.WriteLine();
int[] al2 = new int[] { 11, 33, 2, 5, 10, 10, 10, 15, 63, 64, 54 };
ArrayList arrList = new ArrayList(al2);
//Indexof("A")返回第一次出现的索引位置,未找到返回-1
Console.WriteLine("第一次出现10的索引位置为:{0} ", arrList.IndexOf(10));
//IndexOf("A", 7)从索引7开始查找,未找到返回-1,包含7这个索引位置
Console.WriteLine(arrList.IndexOf(10, 5));//=5;
//LastIndexOf(10)从后面开始检索10第一次出现的索引
Console.WriteLine(arrList.LastIndexOf(10));//=6从
Console.WriteLine(arrList.LastIndexOf(10, 4));//=4
//public virtual bool Contains(Object item)
Console.WriteLine(arrList.Contains(20));//=false
Console.WriteLine(arrList.Contains(10));//=true
}
}
}
属性


方法
传多参


ref

out
重载


结构

类




