Method Overloading - The Complete C# Tutorial

C# Tutorial TOC Table of Contents

Download PDF + Code samples

PDF Download this entire tutorial as PDF, plus all the code samples, right now!

Chapter/article TOC

  1. Introduction to C# classes
  2. Fields
  3. Properties
  4. Methods (functions)
  5. Method parameters
  6. Constructors and destructors
  7. Method overloading
  8. Visibility
  9. Static members
  10. Inheritance
  11. Abstract classes
  12. More abstract classes
  13. Interfaces
  14. Namespaces
  15. Constants (the const keyword)
  16. Partial Classes
  17. Local functions

Getting started

  • Introduction
  • Visual Studio Community
  • Hello, world!
  • Hello world explained

The Basics

  • Variables
  • Data types
  • Code Comments

Control Structures

  • The if statement
  • The switch statement
  • Loops

Classes

  • Introduction to C# classes
  • Fields
  • Properties
  • Methods (functions)
  • Method parameters
  • Constructors and destructors
  • Method overloading
  • Visibility
  • Static members
  • Inheritance
  • Abstract classes
  • More abstract classes
  • Interfaces
  • Namespaces
  • Constants (the const keyword)
  • Partial Classes
  • Local functions

Collections

  • Arrays
  • Lists
  • Dictionaries

Data types

  • Introduction
  • Booleans
  • Integers
  • Floating points
  • The Char type
  • Strings
  • Working with Dates & Time
  • Nullable types
  • Implicitly typed variables (the var keyword)
  • The dynamic Type
  • The ExpandoObject
  • Anonymous Types

Operators

  • Introduction
  • Comparison operators
  • Increment/decrement operators
  • Addition assignment operators
  • The NULL coalescing operator
  • The String Interpolation Operator

LINQ

  • Introduction
  • LINQ: Query Syntax vs. Method syntax
  • Filtering data: the Where() method
  • Sorting data: the OrderBy() & ThenBy() methods
  • Limiting data: the Take() & Skip() methods
  • Data transformations: the Select() method
  • Grouping data: the GroupBy() Method

Working with Culture & Regions

  • Introduction
  • Application Culture & UICulture
  • The CultureInfo class
  • The RegionInfo class

Regular Expressions (Regex)

  • Introduction
  • Searching with the Regex Class
  • Search/Replace with the Regex Class
  • Regex Modifiers

Misc

  • Randomness with the Random class
  • Starting applications with the Process class

Debugging

  • Introduction to debugging
  • Breakpoints
  • Stepping through the code
  • The tool windows
  • Advanced breakpoints

Advanced topics

  • Enumerations
  • Exception handling
  • Structs

XML

  • Introduction to XML with C#
  • Reading XML with the XmlReader class
  • Reading XML with the XmlDocument class
  • Working with the XmlNode class
  • Using XPath with the XmlDocument class
  • Writing XML with the XmlWriter class
  • Writing XML with the XmlDocument class

C# 3.0

  • Introduction to C# 3.0
  • Automatic properties
  • Object Initializers
  • Collection Initializers
  • Extension Methods

File handling

  • Reading and writing files
  • Manipulating files and directories
  • File and directory information

Data Streams

  • Introduction
  • MemoryStream

Reflection

  • Reflection introduction
  • The right Type
  • Instantiating a class
  • A Reflection based settings class
  • C#
  • ASP.NET MVC
  • ASP.NET WebForms
  • CSS3
  • HTML5
  • JavaScript
  • jQuery
  • PHP5
  • WPF

This article is currently in the process of being translated into Vietnamese (~77% done).

If you are fluent in Vietnamese, then please help us - just point to any untranslated element (highlighted with a yellow left border - remember that images should have their titles translated as well!) inside the article and click the translation button to get started. Or have a look at the current translation status for the Vietnamese language.

If you see a translation that you think looks wrong, then please consult the original article to make sure and then use the vote button to let us know about it.

Metadata

Please help us by translating the following metadata for the article/chapter, if they are not already translated.

If you are not satisfied with the translation of a specific metadata item, you may vote it down - when it reaches a certain negative threshold, it will be removed. Please only submit an altered translation of a metadata item if you have good reasons to do so!

Please login to translate metadata! Already logged in? Please try reloading the page!
  • More info...
  • Looking for the original article in English?
Classes: Method overloading

Rất nhiều ngôn ngữ lập trình hỗ trợ tham số mặc định/tùy chọn. Nó cho phép lập trình viên có thể tạo ra một hay vài tham số tùy chọn bằng cách gán cho chúng giá trị mặc định. Đặc biệt là khi thêm chức năng vào code có sẵn.

Ví dụ, bạn có thể muốn thêm chức năng vào một hàm có sẵn, cần một hay vài tham số để đưa vào. Bằng cách đó, bạn có thể làm hỏng chương trình khi gọi hàm này. Để làm việc với nó bạn có thể định nghĩa tham số thêm vào là tùy biến, và gán cho chúng giá trị mặc định tương ứng với cách mà code hoạt động trước khi thêm tham số

Tham số mặc định được giới thiệu trong C# phiên bản 4.0 nhưng trước đó, các lập trình viên C# dùng công nghệ khác, là dùng phương thức xếp chồng. Nó cho phép lập trình viên định nghĩa nhiều phương thức khác nhau có cùng tên khác nhau ở tập tham số. Khi bạn dùng class trong .NET framework, bạn sẽ sớm nhận ra rằng nạp chồng hàm được dùng nhiều. Một ví dụ hay về nó là phương thức Substring() của lớp String. Nó có hàm nạp chồng như sau:

string Substring (int startIndex) string Substring (int startIndex, int length)

Bạn có thể gọi nó với cả một hay hai tham số. Nếu bạn chỉ gọi với một tham số thì tham số length được cho là chiều dài chuỗi con, tiết kiệm thời gian bất cứ khi nào chúng ta muốn lấy phần còn lại của chuỗi

So, by defining several versions of the same function, how do we avoid having the same code several places? It's actually quite simple: We let the simple versions of the method make the complex version of it do all the work. Consider the following example:

class SillyMath { public static int Plus(int number1, int number2) { return Plus(number1, number2, 0); } public static int Plus(int number1, int number2, int number3) { return number1 + number2 + number3; } }

Chúng ta định nghĩa phương thức Plus với hai phiên bản. Phiên bản đầu tiên có hai tham số để cộng hai số trong khi phiên bản thứ hai có ba tham số. Công việc thực sự được hoàn thành ở phiên bản ba tham số - nếu chúng ta chỉ muốn cộng hai tham số thì chúng ta chỉ cần gọi phương thức ba tham số và dùng 0 như là tham số thứ ba, giống giá trị mặc định. Tôi biết, tôi biết, đó là một ví dụ có vẻ ngốc nghếch như được chỉ định bởi tên của lớp nhưng nó sẽ cho bạn ý tưởng về cách hoạt động của xếp chồng.

Now, whenever you feel like doing advanced math by adding a total of four numbers (just kidding here), it's very simple to add a new overload:

class SillyMath { public static int Plus(int number1, int number2) { return Plus(number1, number2, 0); } public static int Plus(int number1, int number2, int number3) { return Plus(number1, number2, number3, 0); } public static int Plus(int number1, int number2, int number3, int number4) { return number1 + number2 + number3 + number4; } }

The cool thing about this, is that all your existing calls to the Plus method will continue working, as if nothing had been changed. The more you use C#, the more you will learn to appreciate method overloading.

Constructors and destructors Previous Visibility Next This article has been fully translated into the following languages:
  • Chinese
  • Czech
  • Dutch
  • German
  • Hungarian
  • Italian
  • Portuguese
  • Russian
  • Spanish
  • Thai
Is your preferred language not on the list? Click here to help us translate this article into your language!

Từ khóa » Ví Dụ Về Overloading