(3)方法和参数

1.命名空间(using)、类型名称(console)、作用域、方法名称、参数、方法返回。

2.方法的声明

3.       using指令

嵌套的using指令

使用别名

           using Timer =System.Timers.Timer

    ……

           Timer time;

4.       参数

Main() 的返回值和参数

           System.Environment.GetCommandLineArgs()方法在Main()之外的方法中获取命令行参数。

           消除多个Main()方法的歧义:假如一个程序的两个类中都包含Main(),可以在命令行中输入 csc.exe  /m 方法确定具体哪一个类包含了程序的入口点。

参数 :值和引用(ref)

输出参数(out)

参数数组(params):将每个参数作为参数的一个成员来访问。

参数数组不一定是方法声明中唯一的参数,但一定是最后一个参数。

调用者可以为参数数组制定0个参数,这会造成一个包含0个参数的数组。

参数数组是类型安全的。

调用者可以显示的使用一个数组。

Params在方法中定义时使用,调用该方法时不用关键字params。

 

Static string Combine(params string[] paths){……}

 

fullName=Combine(Directory.GetCurrentDirectory(),”bin”,”config”,”index.html”);

fullName=Combine(Environment.SystemDirectory,”temp”,”index.html”);

fullName=Combine(new string[]{“C:\”,”Data”,”HomeDir”});

 

         **递归

                   返回一个目录中所有的.cs文件宝航的代码行数。

   static void Main(string[] args)

        {

            int totalLineCount = 0;

            string directory;

            if (args.Length > 0)

            {

                directory = args[0];

            }

            else

            {

                directory = Directory.GetCurrentDirectory();

            }

            totalLineCount = DirectoryCountLines(directory);

            System.Console.WriteLine(totalLineCount);

            Console.ReadKey();

        }

 

       static int DirectoryCountLines(string directory)

        {

            int lineCount = 0;

            foreach (string file in Directory.GetFiles(directory, "*.cs"))

            {

                lineCount += CountLines(file);

            };

            foreach (string subDirectory in Directory.GetDirectories(directory))

            {

                lineCount += DirectoryCountLines(subDirectory);

            }

 

            return lineCount;

        }

 

       private static int CountLines(string file)

       {

           string line;

           int lineCount = 0;

           FileStream stream = new FileStream(file,FileMode.Open);

           StreamReader reader = new StreamReader(stream);

           line = reader.ReadLine();

 

           while (line != null)

           {

               if (line.Trim() != "")

               {

                   lineCount++;

               }

               line = reader.ReadLine();

           }

           reader.Close();

           return lineCount;

       }


 

                 

5.       方法重载

参数个数、参数类型不同,方法名相同。(多态的体现)

**可选参数:方法声明时直接将常量值赋给参数,以后调用方法时就不必每一个参数都指定。可选参数放在其他参数的后面。

           int  a( string b,string c=”csc”)

**命名参数:调用者可以显示的指定参数名,并为该参数赋一个值。

           DisplayGreeting(fileName:”Inigo”,lastName:”Montoya”);

6.       异常处理

A.   try

{……}

catch()

{……}

catch          //可以指定一个泛化的catch块(generic catch block),不获取任何

{……}           参数。等价于获取object数据类型的catch块。甚少用。

finally

{……}

B .    throw

 

     //重新引发一个异常

     catch(Exception exception)

{

            Console.writeLine(“……”);

throw;                         //在throw后面不指定任何异常。

}

C.使用TryParse()来执行数值转换。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值