Danh mục

Addison Essential Csharp_2

Số trang: 98      Loại file: pdf      Dung lượng: 1.76 MB      Lượt xem: 5      Lượt tải: 0    
Jamona

Phí tải xuống: 33,000 VND Tải xuống file đầy đủ (98 trang) 0
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

về lập trình C # cho đến nay bạn sẽ có thể viết các chương trình đơn giản bao gồm một danh sách các báo cáo, tương tự như các chương trình được tạo ra vào những năm 1970. Lập trình đã đi một chặng đường dài từ những năm 1970, như các chương trình trở nên phức tạp hơn
Nội dung trích xuất từ tài liệu:
Addison Essential Csharp_2 4Methods and ParametersF about C# programming so far you ROM WHAT YOU HAVE LEARNED should be able to write straightforward programs consisting of a list ofstatements, similar to the way programs were created in the 1970s. Pro-gramming has come a long way since the 1970s; as programs became morecomplex, new paradigms were needed to manage that complexity. “Proce-dural” or “structured” programming provides a construct into whichstatements are grouped together to form a unit. Furthermore, with struc-tured programming, it is possible to pass data to a group of statements andthen have data returned once the statements have executed. This chapter covers how to group statements together into a method. Inaddition, it covers how to call a method, including how to pass data to amethod and receive data from a method. Namespace Type Name Exception Calling 6 1 Handling a Method Scope Method Name Parameters Method Return Method Declaring 5 2 Methods and Overloading a Method Parameters Value Parameters The Using 3 4 Parameters Directive Reference Parameters (ref) Aliasing Output Parameters (out) Parameter Arrays (params) Optional Parameters 149 From the Library of Wow! eBook150 C hapter 4: Methods and Parameters Besides the basics of calling and defining methods, this chapter also covers some slightly more advanced concepts—namely, recursion and method overloading, along with some new C# 4 features, namely optional and named parameters. All method calls discussed so far and through the end of this chapter are static (a concept which Chapter 5 explores in detail). Even as early as the HelloWorld program in Chapter 1, you learned how to define a method. In that example, you defined the Main() method. In this chapter, you will learn about method creation in more detail, including the special C# syntax for parameters that pass data to and from a method (ref) using a single parameter, as well as parameters that only pass data out from a method (out). Lastly, I will touch on some rudimen- tary error handling. Calling a Method BEGINNER TOPIC What Is a Method? Up to this point, all of the statements in the programs you have written have appeared together in one grouping called a Main() method. As pro- grams become even minimally larger, a single method implementation quickly becomes difficult to maintain and complex to read through and understand. A method is a means of grouping together a sequence of statements to perform a particular action or compute a particular result. This provides greater structure and organization for the statements that comprise a pro- gram. Consider, for example, a Main() method that counts the lines of source code in a directory. Instead of having one large Main() method, you can provide a shorter version that allows you to hone in on the details of each method implementation as necessary. Listing 4.1 shows an example. Listing 4.1: Grouping Statements into Methods class LineCount { static void Main() { int lineCount; string files; From the Library of Wow! eBook 151 C alling a Method D ...

Tài liệu được xem nhiều: