c#学习(基础2)

9  while

    int max = 10;
           int i = 0;
           while(true)
           {
                Console.WriteLine("this isthe " + i);
                i++;
                if(i == max){
                    break;
                }
           }
           Console.ReadKey();

10for 循环

for (int i = 0; i <= 20;i++ )
            {
                Console.WriteLine("this isthe " + i);
           }
 
           Console.ReadKey();


11 类型转换 cast

int i = 10000;
           byte b = (byte)i;
           Console.WriteLine(b);


12 枚举

enum Gender { male, female, un };
 Gender g = Gender.female;
 Console.WriteLine(g);

13 数组

声明数组

 int[] values1 = {3,2,1};

  intvalues1[] = {3,2,1};   这样不可以!!

      int[] values1 = {3,2,1};
         for(inti=0;i<values1.Length;i++){
                Console.WriteLine(values1[i]);
           }

二维数组:

        int[,] arr = new int[2, 3];

            int[,] arr1 = {
                            {1,2},
                            {3,4}
                          };
遍历:

static void Main(string[] args)
        {
            int[,] arr = new int[2, 3];

            int[,] arr1 = {
                            {1,2},
                            {3,4}
                          };
            OutArr(arr1);
            Console.ReadKey();
        }

        static void OutArr(int[,] arr)
        { 
            for(int i=0;i<arr.Rank;i++)
            {
                for(int j=0;j<arr.GetUpperBound(arr.Rank -1) + 1;j++)
                {
                    Console.Write(arr[i,j] + " ");
                }
                Console.WriteLine();
            }
        }

14  foreach

string[] names ={"aa","vv","asd"};

           foreach(stringnamein names)

           {

                Console.WriteLine(name);   

           }

 

15 函数

static void Main(string[] args)
       {
           string[] names = { "a","b","c"};
           Out(names);
           Console.ReadKey();
       }
 
      static void Out(string[] names)
       {
           foreach(string name in names)
           {
                Console.WriteLine(name);
            }
       }


 

16函数可变参数

 Params 是可变参数的关键字

static void Out(paramsstring[] names)

       {

           foreach (string name in names)

           {

                Console.WriteLine(name);

           }

       }

 

调用:

Out("a","v");

Out("a","v","d");

 

 

17 函数重载参数类型不同或者参数个数不同 与返回值无关!!

 

static voidsay(string word)
       {
           Console.WriteLine(word);
       }
 
       static void say(int w)
       {
           Console.WriteLine(w);
       }

18 字符串处理’

 

(1)单引号中只能  放入一个字符

(2)单个字符也可以表示为字符串,还可以有长度为0的字符串

         Eg    string s = “a”

                            Strings = “”;

(3)使用s.Length获取字符串的字符个数

(4)string可以看做成char的只读数组,char c = s[1] 就是获取第二个 字符

(5)c#中字符串有一个重要的特性,不可变性,字符串一旦声明,必须改变,所以只能通过索引来读取指定位置的char,不能对指定位置的char进行修改

如果要对char进行修改,那么就必须创建一个新的字符串,用s.ToCharArray()方法得到字符串的char数组,对数组进行修改后,调用new string(char[])这个函数来创建字符串

Eg

 

           string s = "abcde";

           char[] arr = s.ToCharArray();

           arr[0] = 'x';

           string ss = new string(arr);

 

           Console.WriteLine(s);// abcde

           Console.WriteLine(ss);//xbcde

 

(6)string常用函数

见C#.NET函数(字符串函数)

 

19ref out

 

函数参数默认是值传递的,也就是“复制一份”

 

ref 必须先初始化,因为是引用,所以必须先“有”,才能引用,而out则是内部为外部赋值,所以不需要初始化,而且外部初始化也没有用。

Ref应用场景内部对外部的值进行改变,out则是内部为外部变量赋值,

Out一般使用在函数有多个返回值的场所。

 

Eg

 

1
             tring s = "hello ";

       tring s = "hello ";
       reff(ref s);//ref 引用!
       Console.WriteLine(s);//输出结果是 hello world

           reff(ref s);//ref 引用!

           Console.WriteLine(s);//输出结果是 hello world

 

    static void reff(ref strings) //写参数时,ref也传入

    {

       s = s + "world";

    }

 

2

   static void reff(string s)

    {

       s = s + "world";

}

 

                         strings = "hello ";

            reff( s);

            Console.WriteLine(s);//结果 是 hello  因为传递的值!! 即使是string也是传值,这个与java的string不一样

 

3

       static void reff(out string s)

    {

        s = "h";//因为在参数用使用out所以必须对s赋值!

        s = s + "world";

    }

   string s = "hello ";

   reff(out s);

   Console.WriteLine(s);

 

out的经常的使用地方

 string s = Console.ReadLine();
           int i;
           if (int.TryParse(s, out i))
           {
                Console.WriteLine("success{0}",i);
           }
           else
           {
               Console.WriteLine("failture {0}",i);
           }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值