[C #] the basis of three interfaces

#region 数据类型
byte b = 255; //byte范围:0-255
short s = -32768; //short范围:-32768-32767
int i = 2;
long p = 1222222;
float f = 1.1F; //单精度的数值后面要加F
double d = 1.11; //double类型不用加后缀。范围包括以上所有类型的值
decimal de = 1.1M; //银行项目用,精度高
string st = "Hello World";
st = null; //st中什么都没有
st = "";   //st中有个字符串,但这个字符串为空字符串
MessageBox.Show(st);
#endregion
 #region 一元表达式 自增量 自减量
  int a = 2;
  --a;                   //a自减1
  MessageBox.Show(a.ToString());

  int a1 = 2;
  ++a1;                 //a1自加1
  MessageBox.Show(a1.ToString());

  int a2 = 2;
  a2++;                 //a2自加1
  MessageBox.Show(a2.ToString());

  int a3 = 2;
  a--;                 //a3自减1
  MessageBox.Show(a3.ToString());

  //区别: ++在前和++在后
  int b = 1;
  MessageBox.Show((b++).ToString()); //1【在此句话执行结束后,再执行自加】

  int c = 1;
  MessageBox.Show((++c).ToString()); //2 【先执行自加,再执行此句话】

  #endregion
  #region 二元运算符 x??y
   //如果x为null,则为y,否则为x

   string a, b, c;
   a = null;
   b = "hello";
   c = a ?? b;
   MessageBox.Show(c);

   string a1, b1, c1;
   a1 = "ok";
   b1 = "hello";
   c1 = a1 ?? b1;
   MessageBox.Show(c1);
   #endregion
#region 二元运算符 且&& 或||
 bool a;
 bool b1 = true, b2 = false, b3 = true, b4 = false;
 a = b1 && b2;
 MessageBox.Show(a.ToString()); //false
 a = b1 || b2;
 MessageBox.Show(a.ToString()); //true
 a = b1 && b3;
 MessageBox.Show(a.ToString()); //true
 a = b2 || b4;
 MessageBox.Show(a.ToString()); //false

 #endregion
 #region 三元运算符 x?y:z
  //如果x为true,则y,否则为z

  string x = "a", y = "hello", z = "world", m;
  m = x == "a" ? y : z;
  MessageBox.Show(m);
  m = x == "b" ? y : z;
  MessageBox.Show(m);
  #endregion
 #region 非运算符 !
 bool b = true;
 MessageBox.Show(b.ToString());
 MessageBox.Show((!b).ToString());
 #endregion
#region 字符串运算
string s1 = "Hello", s2 = "World", res;
res = s1 + s2;  //字符串拼接
MessageBox.Show(res);
#endregion
 #region switch case
 int i = 4;
 switch (i)
 {
     case 1:
         MessageBox.Show("1");
         break;
     case 2:
         MessageBox.Show("2");
         break;
     case 3:
         MessageBox.Show("3");
         break;
     default:
         MessageBox.Show("null");
         break;
 }
 #endregion
 #region for 循环
 for (int i = 0; i < 5; i++) //按tab键,会自动补全
 {
     MessageBox.Show("这是第" + (i + 1) + "次循环");
 }
 #endregion
#region while循环
 int i = 0;
 while (i < 10)
 {
     MessageBox.Show(i.ToString());
     i++;
 }
 #endregion
 #region  do while 循环
 int i = 1;
 do
 {
     MessageBox.Show(i.ToString());
     i++;
 }
 while (i < 5);
 #endregion
#region 数组 int[]
int[] a = new int[] { 1, 2, 3, 4, 5 };
int a1 = a[0];
MessageBox.Show(a1.ToString());
#endregion
#region 案例二  取整 取余
double totalPrice = 36.3;
double discount = 0.75;
int a = (int)(totalPrice / 10); //只保留整数部分,显示转换
//MessageBox.Show(a.ToString());

int totalPrice2 = 36;
int b = totalPrice2 / 10;      //只保留整数部分
//MessageBox.Show(b.ToString());

double totalTen = 0;
for (int i = 0; i < b; i++)
{
    totalTen += 10 * discount;
};
MessageBox.Show(totalTen.ToString());
double totalDiscountPrice = 0;
totalDiscountPrice = totalTen + totalPrice2 % 10;
MessageBox.Show(totalDiscountPrice.ToString());
#endregion
#region Arraylist 添加、修改、插入、移除
ArrayList arrayList = new ArrayList();
arrayList.Add(123); //添加
arrayList.Add("abc");
arrayList[0] = 345; //修改
arrayList.Insert(1, 123 + "abc"); //插入 隐式转换,“123abc”
arrayList.RemoveAt(0);//移除第0个
arrayList.Remove("abc");//移除“abc”
#endregion
 #region List 集合
 List<int> lst = new List<int>() { 1, 2, 3, 4, 5, 6 };
 lst.Add(1);
 lst.Add(2);
 lst.Add(3);
 lst.Add(4);
 lst.Add(5);
 lst[0] = 11;
 lst.Insert(0, 111);
 lst.RemoveAt(0);
 lst.Remove(4);
 lst.Clear();   //清空集合

 #endregion
 #region Dictionary 字典
 Dictionary<int, string> dic = new Dictionary<int, string>();
 dic.Add(1, "98分");
 dic.Add(2, "99分");
 dic.Add(3, "100分");
 dic[0] = "980分";  //这里不是索引,而是键为0,值为980分
 dic[1] = "1000分"; //修改了键=1的值
 Dictionary<string, string> dic2 = new Dictionary<string, string>  //对象初始化器
 {
     {"a","A" },
     {"b","B" },
     {"c","C" }
 };

 string dic3 = dic[1];
 #endregion
#region foreach循环
int[] a = { 1, 2, 3, 4, 5, 6 };
foreach (int item in a)
{
    MessageBox.Show(item.ToString());
}

List<int> b = new List<int>() { 1, 2, 3, 4, 5 };
foreach (int item in b)
{
    MessageBox.Show(item.ToString());
}

Dictionary<string, string> c = new Dictionary<string, string>()
{
    {"A","a" },
    {"B","b" },
    {"C","c" }
};
foreach (KeyValuePair<string, string> item in c)
{
    MessageBox.Show(item.Key); //打印字典的键
    MessageBox.Show(item.Value); //打印字典的值
}
#endregion
#region 自定义类并调用,通过foeeach循环展示
Person pp = new Person(); //实例化Person,命名为pp
List<Person> ppList = pp.GetUserList();//pp下的方法GetUserList的返回值,赋值给ppList
foreach (Person item in ppList) //参考foreach(int item in a)
{
    MessageBox.Show(item.Name + "的年龄是:" + item.Age);
}
#endregion
Published 10 original articles · won praise 0 · Views 170

Guess you like

Origin blog.csdn.net/weixin_42046939/article/details/104520176