tailieunhanh - Microsoft Visual C# 2010 Step by Step (P6)

Tham khảo sách 'microsoft visual c# 2010 step by step (p6)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 220 Part II Understanding the C Language Using Array Arguments Suppose you want to write a method to determine the minimum value in a set of values passed as parameters. One way is to use an array. For example to find the smallest of several int values you could write a static method named Min with a single parameter representing an array of int values class Util public static int Min int paramList if paramList null 0 throw new ArgumentException not enough arguments int currentMin paramList 0 foreach int i in paramList if i currentMin currentMin i return currentMin Note The ArgumentException class is specifically designed to be thrown by a method if the arguments supplied do not meet the requirements of the method. To use the Min method to find the minimum of two intvalues you write this int array new int 2 array 0 first array 1 second int min array And to use the Min method to find the minimum of three int values you write this int array new int 3 array 0 first array 1 second array 2 third int min array You can see that this solution avoids the need for a large number of overloads but it does so at a price you have to write additional code to populate the array that you pass in. However you can get the compiler to write some of this code for you by using the params keyword to declare a params array. Chapter 11 Understanding Parameter Arrays 221 Declaring a params Array You use the params keyword as an array parameter modifier. For example here s Min again this time with its array parameter declared as a params array class Util public static int Min params int paramList code exactly as before The effect of the params keyword on the Min method is that it allows you to call it by using any number of integer arguments. For example to find the minimum of two integer values you write this int min first second The compiler translates this call into code similar to this int array new int 2 array 0 first array 1 second .

TÀI LIỆU LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.