Constructors And Destructors - The Complete C# Tutorial
Có thể bạn quan tâm
Download PDF + Code samples
Chapter/article TOC
- 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
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
This article is currently in the process of being translated into Vietnamese (~98% 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?
Hàm khởi tạo là phương thức đặc biệt, được dùng để tạo thể hiện lớp. Một hàm khởi tạo có thể không trả về cái gì, đó là lý do bạn không cần khai báo kiểu trả về cho nó. Một hàm khởi tạo thông thường như sau:
public string Describe()Một hàm khởi tạo có thể định nghĩa như sau:
public Car()Trong ví dụ của chúng ta ở chương này, chúng ta có lớp Car với hàm khởi tạo lấy chuỗi là đối số. Tất nhiên, một hàm khởi tạo có thể nạp chồng (overloading), có nghĩa là chúng ta có nhiều hàm khởi tạo với cùng tên nhưng khác tham số. Đây là ví dụ:
public Car() { } public Car(string color) { this.color = color; }Một hàm khởi tạo có thể gọi hàm khởi tạo khác. Ví dụ:
public Car() { Console.WriteLine("Constructor with no parameters called!"); } public Car(string color) : this() { this.color = color; Console.WriteLine("Constructor with color parameter called!"); }Nếu bạn chạy code thì bạn thấy rằng hàm khởi tạo không có tham số sẽ được gọi trước. Nó có thể dùng để khởi tạo nhiều đối tượng khác nhau trong class với hàm khởi tạo mặc định, nó có thể được gọi từ hàm khởi tạo khác trong class. Nếu hàm khởi tạo có tham số thì bạn cũng có thể dùng. Đây là một ví dụ đơn giản:
public Car(string color) : this() { this.color = color; Console.WriteLine("Constructor with color parameter called!"); } public Car(string param1, string param2) : this(param1) { }Nếu bạn gọi hàm khởi tạo có hai tham số thì tham số đầu tiên sẽ được lấy để gọi hàm khởi tạo có tham số đó.
Destructors
Vì C# có cơ chế tự giải phóng, framework tự động giải phóng đối tượng mà bạn không dùng nữa, cũng có một số thứ phải tự giải phóng. Hàm hủy, là phương thức được gọi khi giải phóng đối tượng, có thể được dùng để xóa tài nguyên được dùng bởi đối tượng. Hàm hủy không giống các hàm khác trong C#. Đây là một ví dụ về hàm hủy trong lớp Car:
~Car() { Console.WriteLine("Out.."); }Một khi đối tượng được xóa bởi garbage collector thì phương thức này được gọi.
Method parameters Previous Method overloading Next This article has been fully translated into the following languages:- Czech
- Dutch
- French
- German
- Hungarian
- Italian
- Portuguese
- Russian
- Spanish
- Thai
Từ khóa » Hàm Khởi Tạo Có Tham Số C#
-
Constructor (hàm Tạo/hàm Dựng) Trong C#, Khởi Tạo Object | Tự Học ICT
-
Khởi Tạo Phương Thức Khởi Tạo Trong C Sharp
-
Hàm Khởi Tạo Và Hàm Hủy - Quản Trị Máy Tính
-
Constructor (hàm Tạo/hàm Dựng) Trong C#, Khởi Tạo ...
-
TÌm Hiểu Về Hàm Xây Dựng, Hàm Hủy Trong C# - Freetuts
-
Ngôn Ngữ C# - Constructor
-
Ngôn Ngữ C# - Hàm Khởi Tạo Static
-
Hàm Dựng (constructor) Và Tính Kế Thừa Trong C# - Minh Hoàng Blog
-
Hàm Khởi Tạo Và Hàm Huỷ - Lập Trình Không Khó
-
[PDF] Bài 3 HƯỚNG ĐỐI TƯỢNG TRONG C# - Topica
-
Lập Trình Hướng đối Tượng Với C# - Học Lập Trình Cùng CodeGym
-
Tìm Hiểu C# Và ứng Dụng: Lớp Và đối Tượng Trong C# - VOER
-
C# - Bài 36: Hàm Tạo - Constructor - YouTube
-
Các Hiểu Biết Cơ Bản Về C# - Viblo