zoukankan      html  css  js  c++  java
  • 【C#.Net】方法和参数

     

    Ctrl+Tab---标签切换

    1.using 命名空间名字(将其他文件命名空间中的内容引入,在本文件中就可以直接引用引用的命名空间中的方法和属性了)

    2.方法类型:static方法和非static方法(动态方法)

    例如:静态方法:static void Test()

               动态方法:void Test()

     1)动态方法可以调用:动态/静态方法

     2)静态方法只能调用:静态方法

    有返回值(方法前加int,double...等数据类型)和无返回值(方法前加void)

     1 namespace test5
     2 {
     3     class Program
     4     {
     5         static double Average(List<int>list)
     6         {
     7             int sum = 0;
     8             foreach(var l in list)
     9             {
    10                 sum += l;
    11             }
    12             return (double)sum / list.Count;
    13         }
    14         static void Main(string[] args)
    15         {
    16             var list = new List<int>();
    17             list.Add(1);
    18             list.Add(2);
    19             list.Add(3);
    20 
    21             
    22             Console.WriteLine(Average(list));
    23             Console.Read();
    24         }
    25     }
    26 }
    View Code

    1)传值

    值传递:main方法中的a和b传值给Swap中的a和b(传值相当于复制一遍被传的值,放到新的内存地址中存储),swap中的a,b值变更,main中的a,b值不会随之变化,仍是之前的值。

     1 namespace test5
     2 {
     3     class Program
     4     {
     5         static void Swap(int a,int b)//list为形式参数,可以不叫list,比如叫nums也可以,但是此方法内部对应需要改为nums
     6         {
     7             int t;
     8             t =a;a = b;b = t;
     9             Console.WriteLine("a={0},b={1}", a, b);//list为实际参数
    10 
    11         }
    12         static void Main(string[] args)
    13         {
    14             int a = 1;
    15             int b = 2;
    16             Swap(a, b);
    17             Console.WriteLine("a={0},b={1}", a, b);//list为实际参数
    18 
    19             Console.Read();
    20         }
    21     }
    22 }
    View Code

    运行结果为:

    a=2,b=1

    a=1,b=2

    2)传引用ref--想公用加ref;如果只是传递数字,不共用参数值,不加ref

    a.引用类型(用class做出来的所有类型都是引用类型):string(有个特性叫不可变性,加不加ref的表现同基本类型),list(加不加ref同样效果,都是公用内存空间)

    b.基本类型(加ref公用,不加ref,不共用):char,int,double,bool

    main中a和Swap中的a指向相同的内存地址,如果修改Main中a的值,Swap中a的值也会随之变化。b的值同理。

    不像上方传值是复制的关系,变成公用关系了

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace test5
     8 {
     9     class Program
    10     {
    11         static void Swap(ref int a,ref int b)//ref代表Swap中的a,b同main中的a,b共同用a,b的值
    12         {
    13             int t;
    14             t =a;a = b;b = t;
    15             Console.WriteLine("a={0},b={1}", a, b);//list为实际参数
    16 
    17         }
    18         static void Main(string[] args)
    19         {
    20             int a = 1;
    21             int b = 2;
    22             Swap(ref a, ref b);
    23             Console.WriteLine("a={0},b={1}", a, b);//list为实际参数
    24 
    25             Console.Read();
    26         }
    27     }
    28 }
    View Code

    运行结果:

    a=2,b=1

    a=2,b=1

    3)out参数---函数理论上只能返回一个值,但是在函数括号中追加添加(,out 变量名)后,就可以返回多个值

    比如tryparse---尝试解析,就可以用到out;

    int TyParse("1",out i);-------理论上TryParse返回值为Boolean类型,但是如果我们还想得到转换成功后的值就可以用out去把值返回回来。

    再比如方法中传参static void Add(int i,out boolean success)---想知道Add()是否执行成功,可以追加一个参数success,Add方法中可以将success复制为true。证明传值成功。out为Add函数返回给main的另一个追加返回值

    ? ref同out区别

    ref:参数数量不改变,变量前加不加ref,值是否改变根据数据类型来判断。list(引用类型)加不加ref,值都同加ref效果一样。char,int,double,string(以上为基本类型),加ref,同名方法中的参数值指向同一个内存空间,值共用(即一个方法中参数值改变,其他同名方法中的对应参数值随之改变,因为他们参数都指向同一个内存空间,而相同内存空间中的值都是同一个),不加ref(即传值),值不共用,效果参看传值,即同名方法中的值存在于不同内存空间,引用的时候值进行了复制且存储到不同内存空间,由于存储到的是不同内存空间,后续一个方法中参数值改变,其他方法中的参数值不会随之改变。

    -------总之可以理解为ref是为基本类型设计的,虽然string为引用类型,在ref这里,可以把string理解为基本类型。(也可以同指针的概念联想理解,即基本类型前加ref,相当于指针指向同一个内存空间,不加ref,相当于指针指向不同内存空间)

    out:追加返回值时用,参数数量使用out后增加,通过使用out可以追加一个或多个函数返回值。

    重载---方法名相同但是其中的参数类型不同(*)

     泛型---别的语言中也有叫模板的,即同样的复杂的值替换成字母只需要在方法后添加个<>即可(也可以不加<>,编译器可以根据参数的赋值自己推断出类型),方法,接口,类,catch中都可以使用到

    可以在Main方法引用方法的时候规定好参数类型,方法中可以用一个字母代替即可,方法中不用每个参数都写数据类型了,去除重复复

    例如:

     下面的写法也可以,编译器是可以自己推断出Swap中的a,b参数是string类型的。

     

     

     1 namespace test5
     2 {
     3     class Program
     4     {
     5         static int Sum(params int[] nums)//ref代表Swap中的a,b同main中的a,b共同用a,b的值
     6         {
     7             return nums.Sum();
     8 
     9         }
    10         static void Main(string[] args)
    11         {
    12            
    13             Console.WriteLine(Sum(1,2,3,4,5,7));//list为实际参数
    14 
    15             Console.Read();
    16         }
    17     }
    18 }
    View Code

     递归---不要纠结细节,了解逻辑去写即可。

    递归的种植条件是return 1;如果没有return 1;就是无限调用。

     统计文件数:file(文件),directory(文件夹)

     统计文件总数:file(文件)和directory(文件夹)总数写法

     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace test5
     9 {
    10     class Program
    11     {
    12         static long FileOrDirCount(string path)//ref代表Swap中的a,b同main中的a,b共同用a,b的值
    13         {
    14             long count = 0;
    15 
    16             //按两下Tab自动补齐函数
    17             try
    18             {
    19                 //统计file(文件)的个数
    20                 var files = Directory.GetFiles(path);
    21                 count += files.Length;
    22 
    23                 //统计directory(文件夹)的个数
    24                 var dirs = Directory.GetDirectories(path);
    25                 count += dirs.Length;//文件夹统计出来的数字基础上再加上统计出来的文件夹数
    26                 foreach (var dir in dirs)
    27                 {
    28                     count += FileOrDirCount(dir);
    29                 }
    30             }             
    31 
    32             catch (Exception ex)
    33             {
    34                 Console.WriteLine(ex.Message);
    35 
    36                // throw;//如果加上throw是抛出异常让main继续处理,我们这里不需要,所以注释掉
    37             }
    38             
    39 
    40            
    41             return count;            
    42 
    43         }
    44         static void Main(string[] args)
    45         {
    46           
    47                 Console.WriteLine("正在统计中...");//list为实际参数
    48 
    49                 Console.WriteLine(FileOrDirCount("F:/"));//list为实际参数
    50 
    51 
    52             Console.Read();
    53         }
    54     }
    55 }
    View Code

    只统计directory(文件夹)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace test5
     9 {
    10     class Program
    11     {
    12         static long FileOrDirCount(string path,bool onlyDir)//ref代表Swap中的a,b同main中的a,b共同用a,b的值
    13         {
    14             long count = 0;
    15 
    16             //按两下Tab自动补齐函数
    17             try
    18             {
    19                 if(!onlyDir)
    20                 {
    21                     //统计file(文件)的个数
    22                     var files = Directory.GetFiles(path);
    23                     count += files.Length;
    24                 }            
    25 
    26                 //统计directory(文件夹)的个数
    27                 var dirs = Directory.GetDirectories(path);
    28                 count += dirs.Length;//文件夹统计出来的数字基础上再加上统计出来的文件夹数
    29                 foreach (var dir in dirs)
    30                 {
    31                     count += FileOrDirCount(dir, onlyDir);
    32                 }
    33             }             
    34 
    35             catch (Exception ex)
    36             {
    37                 Console.WriteLine(ex.Message);
    38 
    39                // throw;//如果加上throw是抛出异常让main继续处理,我们这里不需要,所以注释掉
    40             }
    41             
    42 
    43            
    44             return count;            
    45 
    46         }
    47         static void Main(string[] args)
    48         {
    49          
    50                 Console.WriteLine("正在统计中...");//list为实际参数
    51 
    52                 Console.WriteLine(FileOrDirCount("F:/",true));//list为实际参数
    53 
    54 
    55             Console.Read();
    56         }
    57     }
    58 }
    View Code

    仅统计F盘中后缀为.cs的文件数量,extension代表后缀为.cs的文件

     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace test5
     9 {
    10     class Program
    11     {
    12         static long FileOrDirCount(string path,bool isDir,bool isFile,string extension)//ref代表Swap中的a,b同main中的a,b共同用a,b的值
    13         {
    14             long count = 0;
    15 
    16             //按两下Tab自动补齐函数
    17             try
    18             {
    19                 if(isFile)
    20                 {
    21                     //统计file(文件)的个数
    22                     var files = Directory.GetFiles(path);
    23                     foreach(var file in files)
    24                     {
    25                         if(file.ToLower().EndsWith(extension)||string.IsNullOrEmpty(extension))//如果extension有值或者没提供值时,都count++,进行统计文件
    26                         {
    27                             count++;
    28                         }
    29                     }
    30                 }            
    31 
    32                 //统计directory(文件夹)的个数
    33                 var dirs = Directory.GetDirectories(path);
    34 
    35                 if (isDir)
    36                 {
    37                     count += dirs.Length;//文件夹统计出来的数字基础上再加上统计出来的文件夹数
    38                 }
    39                 foreach (var dir in dirs)
    40                 {
    41                     count += FileOrDirCount(dir, isDir,isFile,extension);
    42                 }
    43             }             
    44 
    45             catch (Exception ex)
    46             {
    47                 Console.WriteLine(ex.Message);
    48 
    49                // throw;//如果加上throw是抛出异常让main继续处理,我们这里不需要,所以注释掉
    50             }
    51             
    52 
    53            
    54             return count;            
    55 
    56         }
    57         static void Main(string[] args)
    58         {
    59          
    60                 Console.WriteLine("正在统计中...");//list为实际参数
    61 
    62                 Console.WriteLine(FileOrDirCount("F:/",false,true,".cs"));//list为实际参数
    63 
    64 
    65             Console.Read();
    66         }
    67     }
    68 }
    View Code

    默认参数

     

     谁调用Main方法:

     重新生成解决方案:

    打开Main方法所在文件所在的路径

     把test5.exe文件直接拖拽到cmd框中,回车

     微软的帮助库:https://docs.microsoft.com/zh-cn/dotnet/

    goto推荐应用场景---1.跳出多层循环,2.switch实现贯通只能用goto

    goto---goto end(有goto end才能本次执行结束,否则下面代码还会继续执行)

     

  • 相关阅读:
    Reversion windows 2008 R2 STD to Datacenter
    NetAPP常用操作
    firefox解决flash崩溃
    物理和虚拟兼容性RDM的区别
    网络嗅探器Wireshark
    子网掩码在线计算换算及算法
    Debian中文字体安装
    快算24点,POJ(3983)
    第九十八周,搜索24点
    两次DFS,POJ(1481)
  • 原文地址:https://www.cnblogs.com/zhuzhubaoya/p/12384777.html
Copyright © 2011-2022 走看看