part01.01 C#基础语法与逻辑控制(三)(4. 方法和参数 5. 异常和错误处理)

C# 方法和参数

基本概念

方法声明 

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string firstName;
 6             string lastName;
 7             string fullName;
 8             string initials;
 9 
10             System.Console.WriteLine("你好:");
11 
12             firstName = GetUserInput("请输入你的姓氏:");
13             lastName = GetUserInput("请输入你的名字:");
14 
15             fullName = GetFullName(firstName, lastName);
16             initials = GetInitials(firstName, lastName);
17             DisplayGreeting(fullName, initials);
18 
19             Console.ReadKey();
20         }
21 
22         static string GetUserInput(string prompt)
23         {
24             System.Console.Write(prompt);
25             return System.Console.ReadLine();
26         }
27 
28         static string GetFullName(string firstName, string lastName) => $"{ firstName } { lastName }";
29 
30         static void DisplayGreeting(string fullName, string initials)
31         {
32             System.Console.WriteLine($"你好 { fullName },你的简称是: { initials }");
33             return;
34         }
35 
36         static string GetInitials(string firstName, string lastName)
37         {
38             return $"{ firstName[0] }. { lastName[0] }.";
39         }
40     }
DeclaringAMethod

方法参数

 A. 值参数

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string fullName;
 6             string driveLetter = "C:";
 7             string folderPath = "Data";
 8             string fileName = "index.html";
 9 
10             fullName = Combine(driveLetter, folderPath, fileName);
11 
12             Console.WriteLine(fullName);
13             Console.ReadKey();
14         }
15 
16         static string Combine(string driveLetter, string folderPath, string fileName)
17         {
18             string path;
19             path = string.Format("{1}{0}{2}{0}{3}",
20                 System.IO.Path.DirectorySeparatorChar,
21                 driveLetter, folderPath, fileName);
22             return path;
23         }
24 
25     }
ValueParameters

B. 引用参数(ref)

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string first = "hello";
 6             string second = "goodbye";
 7             Swap(ref first,ref second);
 8             Console.WriteLine($@"first=""{first}"",second=""{second}""");
 9         }
10         static void Swap(ref string x,ref string y)
11         {
12             string temp = x;
13             x = y;
14             y = temp;
15         }
16     }
View Code

C. 输出参数(out)

 1   class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             int[] myArray = new int[5] { 56, 89, 425, 21, 21 };
 6             int maxIndex;
 7             Console.WriteLine("数组中最大的值为:{0}", MaxIndex(myArray, out maxIndex));
 8 
 9             //调用MaxIndex方法后  maxIndex的值为2
10             Console.WriteLine("数组中最大的值是第{0}元素", maxIndex + 1);
11             Console.ReadKey();
12         }
13 
14         static int MaxIndex(int[] m_Array, out int m_Index)
15         {
16             m_Index = 0;
17             int m_Max = m_Array[0];
18 
19             for (int i = 0; i < m_Array.Length; i++)
20             {
21                 if (m_Array[i] > m_Max)
22                 {
23                     m_Max = m_Array[i];
24                     m_Index = i;
25                 }
26             }
27 
28             return m_Max;
29         }
30 
31     }
View Code

方法重载

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string fullName;
 6             string driveLetter = "C:";
 7             string folderPath = "Data";
 8             string fileName = "index.html";
 9 
10             fullName = Combine(driveLetter, folderPath, fileName);
11 
12             Console.WriteLine(fullName);
13             Console.ReadKey();
14         }
15 
16         static string Combine(string driveLetter, string folderPath)
17         {
18             string path;
19             path = string.Format("{1}{0}{2}{0}{3}",
20                 System.IO.Path.DirectorySeparatorChar,
21                 driveLetter, folderPath, "index.html");
22             return path;
23         }
24 
25         static string Combine(string driveLetter, string folderPath, string fileName)
26         {
27             string path;
28             path = string.Format("{1}{0}{2}{0}{3}",
29                 System.IO.Path.DirectorySeparatorChar,
30                 driveLetter, folderPath, fileName);
31             return path;
32         }
33 
34     }
View Code

异常和错误处理(try / catch)

 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string name;
 6             string ageText;
 7             int age;
 8             int result = 0;
 9 
10             Console.Write("请输入你的姓名:");
11             name = Console.ReadLine();
12 
13             Console.Write("请输入你的年龄:");
14             ageText = Console.ReadLine();
15             try
16             {
17                 age = int.Parse(ageText);
18                 Console.WriteLine($" { name },你的年龄是 { age * 12 } 个月。");
19             }
20             catch (FormatException)
21             {
22                 Console.WriteLine($"你输入的数据是: { ageText },这是个不合要求的数据。");
23             }
24             catch (Exception exception)
25             {
26                 Console.WriteLine($"未知错误: { exception.Message }");
27             }
28             finally
29             {
30                 Console.WriteLine($"结束 { name }");
31             }
32 
33             Console.ReadKey();
34         }
35     }
View Code

C# 语言的异常处理功能有助于处理在程序运行期间发生的任何意外或异常情况。 异常处理功能使用 try、catch 和 finally 关键字来尝试执行可能失败的操作、在你确定合理的情况下处理故障,以及在事后清除资源。 公共语言运行时 (CLR)、.NET Framework/任何第三方库或应用程序代码都可以生成异常。
异常是使用 throw 关键字创建而成。

 

posted @ 2017-08-03 11:14  能哔哔就别唧唧  阅读(249)  评论(0)    收藏  举报