PDA

View Full Version : ref and out



Savage
28-06-2003, 18:11
mình mới bắt đầu học C#, theo đó nếu dùng ref thì ta phải initialize các argument trước khi pass , còn out thì không cần... vậy mô hình chung mình có thể dùng out ở mọi lúc mọi nơi , có thể initialize hoặc chưa initialize đều được cả.... vậy sao còn dùng ref làm gì... ???

White_Rose
28-06-2003, 23:01
Khi bạn sử dụng ref, bạn có thể thay đổi giá trị của argument nhưng argument đó phải được assign value trước rồi để khi cần bạn có thể access đến.
Bạn dùng out nếu bạn chỉ cần assign value mà không có nhu cầu access đến nó -> trước khi truyền vào function, argument đó không nhất thiết phải !=null

Savage
29-06-2003, 00:53
Ok ! Cám ơn bạn.. thêm một thắc mắc nhỏ nữa. Đó là về overriding fucntion .
Tớ đọc sách thì họ viết là muốn override thì PHẢI thêm new vào trước khai báo , nhưng tớ thử bỏ New đi mà vẫn OK là sao nhỉ???
ví dụ trong Inside C# nè ..


using System;

class Employee
{
public void CalculatePay()
{
Console.WriteLine("Employee.CalculatePay()");
}
}

class SalariedEmployee : Employee
{
// The new keyword enables you to override the
// base class' implementation.
new public void CalculatePay() // neu to bo new cho nay thif van lam viecj binhf thuong day !!!
{
Console.WriteLine("SalariedEmployee.CalculatePay()");
}
}

class Poly1App
{
public static void Main()
{
Poly1App poly1 = new Poly1App();

Employee baseE = new Employee();
baseE.CalculatePay();


SalariedEmployee s = new SalariedEmployee();
s.CalculatePay();
}
}

White_Rose
29-06-2003, 02:37
Từ khoá new có tác dụng chỉ ra rằng bạn thực sự và biết chắc chắn rằng mình muốn thi hành function đó chứ không phải vô tình đặt cùng tên -> VS.NET sẽ không tạo ra warning khi compile.

Savage
29-06-2003, 03:22
thanks alot
Vậy tóm lại là thích đặt new hay không llà do mình phải không bạn , và việc có mạt new hay ko hoàn toàn không anh hưởng gì?? minh đã thử và cũng nghĩ vậy... nhưng sao tác giả Viết thì có vẻ chắc chắn phải dùng new nhỉ .

Now, let's say that you want to derive a class from Employee and you want to override the CalculatePay method to do something specific to the derived class. To do this, you need to use the new keyword with the derived class's method definition. Here's the code to show how easy that is:




Bài viết được gửi bởi Savage
Ok ! Cám ơn bạn.. thêm một thắc mắc nhỏ nữa. Đó là về overriding fucntion .
Tớ đọc sách thì họ viết là muốn override thì PHẢI thêm new vào trước khai báo , nhưng tớ thử bỏ New đi mà vẫn OK là sao nhỉ???
ví dụ trong Inside C# nè ..


using System;

class Employee
{
public void CalculatePay()
{
Console.WriteLine("Employee.CalculatePay()");
}
}

class SalariedEmployee : Employee
{
// The new keyword enables you to override the
// base class' implementation.
new public void CalculatePay() // neu to bo new cho nay thif van lam viecj binhf thuong day !!!
{
Console.WriteLine("SalariedEmployee.CalculatePay()");
}
}

class Poly1App
{
public static void Main()
{
Poly1App poly1 = new Poly1App();

Employee baseE = new Employee();
baseE.CalculatePay();


SalariedEmployee s = new SalariedEmployee();
s.CalculatePay();
}
}

White_Rose
29-06-2003, 10:36
need chứ có phải là must đâu :-)
Bạn đọc đoạn sau trong cuốn Visual C#.NET Core Reference:


Virtual Methods
As mentioned, the abstract keyword allows a base class to require the implementation of a method in a derived class. A method declared as virtual in C# serves a similar purpose—allowing, but not requiring, that a subclass provide a new implementation. Here’s an example of declaring a virtual method:


class LandAnimal
{
public virtual void Eat()
{
Chew();
Swallow();
}
}

After a method is declared as virtual in a base class, a subclass indicates its intent to override the base class version of the class by using the override keyword. The override keyword is used with virtual methods exactly as it is used with the abstract methods discussed earlier, as shown here:


class Cat: LandAnimal
{
public override void Eat()
{
PlayWithFood();
Chew();
Swallow();
}
}

When a virtual method is called, the most-derived version of the method is invoked. The most-derived method is determined by looking through the inheritance tree from the actual runtime class to the base classes. The first method discovered is invoked. This is usually, but not always, the desired result. Occasionally, it’s desirable to hide a base class method due to an issue widely known as the “fragile base class problem.” The following chain of events demonstrate this type of problem:

A base class, LandAnimal, is created and deployed.

A subclass, Orangutan, is created and deployed, along with other subclasses of the LandAnimal class.

This framework is wildly successful, so a second version is developed. In this second version, the developers responsible for LandAnimal introduce a new virtual method named Groom.

Unfortunately, the Orangutan class already has a method named Groom and will no longer compile because Groom isn’t declared with the override keyword.

In this example, a change to the base class has caused a maintenance problem in a subclass. The original version of Groom probably looks something like this:


class Orangutan: LandAnimal
{
public void Groom(Orangutan other)
{
other.CleanFur();
other.LookForBugs();
other.EatBugs();
}
}

You can solve this problem in C# by creating the Groom method in Orangutan using the new keyword. In C#, the new keyword is used to hide a virtual base class method. When a method is declared as new, the compiler is notified that this method doesn’t participate when looking for a virtual method, so if a virtual method is added to the base class, the existing code will continue to work correctly:


class Orangutan: LandAnimal
{
new public void Groom(Orangutan other)
{
other.CleanFur();
other.LookForBugs();
other.EatBugs();
}
}

Calls made to the Groom method through a base class reference will call the most-derived version of the Groom method that isn’t decorated with new. Calling Groom through an Orangutan reference will call the original Orangutan.Groom method.