peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76pola petir tersembunyi gates of olympus perihokipgsoft perkuat mahjong ways 2 di akun perihokiperihoki beri pola jitu mahjong wins 3 auto maxwin5 scatter jackpot driver ojolstrategi satpam scatter mahjongstrategi menang mahjong abc1131scatter emas pabrik wonosobomodal tipis scatter jadi cuanrtp mahjong ways supir menanginsting kuat bermain mahjong waysmengungkap keunggulan mahjong waysserunya mahjong bermodal kecil untung melimpahstrategi terbaik untuk menang mahjongterus main terus gacormenang banyak dengan scatter mahjong ways1menggali kehebatan mahjong winsrahasia nurul di mahjong waysstrategi mahjong yang mengubah permainan andateknik menang mahjong ways yang efektif

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

TÌm hiểu về hàm xây dựng, hàm hủy trong c#
CĂN BẢN
NÂNG CAO
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Hàm xây dựng và hàm hủy trong c#

Trong bài học hôm nay chúng ta sẽ cùng tìm hiểu về 2 hàm đặc biệt trong c# đó là hàm xây dựng (Constructor) và hàm hủy (Destructor)

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Hàm xây dựng

Trong C#, constructor là một phương thức đặc biệt được gọi tự động tại thời điểm đối tượng được tạo ra.

Mục đích của hàm xây dựng dùng để khởi tạo dữ liệu cho dữ liệu thành viên.

Constructor phải trùng tên với tên lớp và không có kiểu trả về kể cả kiểu void.

Bài viết này được đăng tại [free tuts .net]

Trong c# có 2 loại hàm xây dựng đó là

  • Hàm xây dựng mặc nhiên
  • Hàm xây dựng có đối số

Hàm xây dựng mặc nhiên

Một constructor không có đối số được gọi là constructor mặc định. Nó được gọi tại thời điểm tạo đối tượng.

Nếu bạn không cung cấp hàm tạo cho lớp của mình, C# sẽ tạo một hàm theo mặc định để khởi tạo đối tượng và đặt các biến thành viên thành các giá trị mặc định tùy thuộc vào kiểu dữ liệu của nó.

Mình sẽ liệt kê giá trị mặc định của một số kiểu dữ liệu hay sử dụng sau:

Kiểu dữ liệu Giá trị mặc định
bool false
float 0.0F
int 0
byte 0
decimal 0M
long 0L
char '\0'
Ví dụ
using System;
namespace ConsoleApp1
{
    class People
    {
        int old;
        string name;
        double height;
        public People()
        {
            Console.WriteLine("Goi ham xay dung mac nhien");
            Console.WriteLine("Name" + name);
            Console.WriteLine("Old: " + old);
            Console.WriteLine("height: " + height);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            People p = new People();
            Console.ReadKey();
        }
    }
}

Và kết quả sau khi thực thi đoạn code trên:

cshaprp cons1 PNG

Lưu ý: nếu chúng ta không tạo bất kỳ hàm xây dựng nào thì chương trình sẽ tự tạo cho chúng ta hàm xây dựng mặc nhiên. Chúng ta chỉ có 1 cách duy nhất để khởi tạo đối tượng.

Hàm xây dựng có tham số

Một constructor có tham số được gọi là hàm xây dựng có tham số.

Nó được sử dụng để cung cấp các giá trị khác nhau cho các đối tượng riêng biệt

Ví dụ
using System;
namespace ConsoleApp1
{
    class People
    {
        int old;
        string name;
        double height;
        public People()
        {
            Console.WriteLine("\n---Goi ham xay dung mac nhien---");
        }
        public People(int old, string name, double height)
        {
            Console.WriteLine("\n---Goi ham xay dung co 3 tham so---");
            this.old = old;
            this.name = name;
            this.height = height;
        }
        public People(int old, string name)
        {
            Console.WriteLine("\n---Goi ham xay dung co 2 tham so---");
            this.old = old;
            this.name = name;
        }
        public void Show()
        {
            Console.WriteLine("Old: " + old + ",\nName: " + name + ",\nHeight: " + height);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            People p = new People();
            p.Show();
            People p1 = new People(20, "Nguyen Van A", 180);
            p1.Show();
            People p2 = new People(18, "Nguyen Van A");
            p2.Show();
            Console.ReadKey();
        }
    }
}

Và kết quả sau khi thực thi đoạn code trên:

a PNG

Như vậy nếu chúng ta định nghĩa bao nhiêu hàm xây dựng, thì chúng ta chỉ có bao nhiêu cách khởi tạo đối tượng. Ở ví dụ trên, chúng ta chỉ định nghĩa 3 hàm xây dựng, vậy nên chúng ta chỉ có 3 cách khởi tạo đối tượng.

2. Hàm hủy

Một hàm hủy hoạt động ngược lại với hàm tạo, Nó phá hủy các đối tượng của các lớp. Nó chỉ có thể được định nghĩa một lần trong một lớp. Giống như các hàm xây dựng, hàm hủy được gọi tự động.

Ví dụ
using System;
namespace ConsoleApp1
{
    class People
    {
        public People()
        {
            Console.WriteLine("\n---Goi ham xay dung mac nhien---");
        }
        ~People()
        {
            Console.WriteLine("\n---Goi ham huy---");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            People p = new People();
            Console.ReadKey();
        }
    }
}

Điểm lưu ý:

  • Hàm hủy là duy nhất cho lớp của nó, tức là không thể có nhiều hơn một hàm hủy trong một lớp.
  • Hàm hủy không có kiểu trả về và có cùng tên với tên lớp
  • Hàm hủy được phân biệt với một hàm xây dựng vì ký hiệu ~trước tên của nó.
  • Hàm hủy không chấp nhận bất kỳ tham số nào và không được sửa đổi hàm hủy
  • Hàm hủy không thể được định nghĩa trong Cấu trúc. Hàm hủy chỉ được sử dụng với các lớp.
  • Hàm hủy không thể bị overloaded hoặc kế thừa. (overload là gì mình sẽ cùng tìm hiểu trong một bài khác)
  • Hàm hủy được gọi khi chương trình thoát.

Destructor được gọi là ngầm định bởi trình thu thập Rác .NET framework và do đó, chúng ta không cần quan tâm đến hàm hủy này làm gì.. :)

3. Lời kết

Như vậy là chúng ta đã cùng tìm hiểu xong về hàm xây dựng và hàm hủy trong c# là gì rồi. 

Chúc các bạn hiểu và vận dụng tốt.

Cùng chuyên mục:

Memento Design Pattern trong C# - Cách triển khai và ví dụ

Memento Design Pattern trong C# - Cách triển khai và ví dụ

Interpreter Design Pattern trong C# - Cách triển khai và ví dụ

Interpreter Design Pattern trong C# - Cách triển khai và ví dụ

Strategy Design Pattern trong C# - Cách triển khai và ví dụ

Strategy Design Pattern trong C# - Cách triển khai và ví dụ

Visitor Design Pattern trong C# - Cách triển khai và ví dụ

Visitor Design Pattern trong C# - Cách triển khai và ví dụ

Command Design Pattern trong C# - Cách triển khai và ví dụ

Command Design Pattern trong C# - Cách triển khai và ví dụ

Observer Design Pattern trong C# - Cách triển khai và ví dụ

Observer Design Pattern trong C# - Cách triển khai và ví dụ

Iterator Design Pattern trong C# - Cách triển khai và ví dụ

Iterator Design Pattern trong C# - Cách triển khai và ví dụ

Proxy Design Pattern trong C# - Cách triển khai và ví dụ

Proxy Design Pattern trong C# - Cách triển khai và ví dụ

Composite Design Pattern trong C# - Cách triển khai và ví dụ

Composite Design Pattern trong C# - Cách triển khai và ví dụ

Bridge Design Pattern trong C# - Cách triển khai và ví dụ

Bridge Design Pattern trong C# - Cách triển khai và ví dụ

Decorator Design Pattern trong C# - Cách triển khai và ví dụ

Decorator Design Pattern trong C# - Cách triển khai và ví dụ

Facade Design Pattern trong C# - Cách triển khai và ví dụ

Facade Design Pattern trong C# - Cách triển khai và ví dụ

Adapter Design Pattern trong C# - Cách triển khai và ví dụ

Adapter Design Pattern trong C# - Cách triển khai và ví dụ

Singleton Design Pattern trong C# - Cách triển khai và ví dụ

Singleton Design Pattern trong C# - Cách triển khai và ví dụ

Prototype Design Pattern trong C# - Cách triển khai và ví dụ

Prototype Design Pattern trong C# - Cách triển khai và ví dụ

Builder Design Pattern trong C# - Cách triển khai và ví dụ

Builder Design Pattern trong C# - Cách triển khai và ví dụ

Tự viết ứng dụng quản lý xe ra vào C# Winforms và Firebase

Tự viết ứng dụng quản lý xe ra vào C# Winforms và Firebase

Trong bài viết này Freetuts chia sẽ cho các bạn một ứng dụng khá thú…

Factory Design Pattern trong C# - Cách triển khai và ví dụ

Factory Design Pattern trong C# - Cách triển khai và ví dụ

Chúng ta sẽ tìm hiểu về Factory Design Pattern trong C# là gì? Cách triển…

Hướng dẫn giải phương trình bậc hai trong C#

Hướng dẫn giải phương trình bậc hai trong C#

Giải phương trình bậc nhất ax + b = 0 trong C#

Giải phương trình bậc nhất ax + b = 0 trong C#

Top