em đã đọc một số bài viết về tham chiếu ref trên diễn đàn nhưng em vẫn không hiểu một số chỗ mong mọi người trong forum chỉ giúp:
Mã:
using System;
namespace NapChongToanTu
{
class teaster
{
public int Triple(int val)
{
return 3 * val;
}
private long Triple(ref long val)
{
return 3 * val;
}
}
class Program
{
public static void Main()
{
teaster t = new teaster();
int x = 5;
int y = t.Triple(x);
Console.WriteLine("x: {0} y: {1}
", x, y);
int lx = 10;
int ly = t.Triple(lx);
Console.WriteLine("lx: {0} ly: {1}", lx, ly);
}
}
Mã:
using System;
namespace ExRef
{
class class1
{
public void DisplayCurrentTime()
{
Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", Date, Month, Year, Hour, Minute, Second);
}
public int GetHour()
{
return Hour;
}
public void GetTime(ref int h, ref int m, ref int s)
{
h = Hour;
m = Minute;
s = Second;
}
public class1(DateTime dt)
{
Year = dt.Year;
Month = dt.Month;
Date = dt.Day;
Hour = dt.Hour;
Minute = dt.Minute;
Second = dt.Second;
}
private int Year;
private int Month;
private int Date;
private int Hour;
private int Minute;
private int Second;
}
class Program
{
static void Main()
{
DateTime now = DateTime.Now;
class1 t = new class1(now);
t.DisplayCurrentTime();
int theHour = 0;
int theMinute = 10;
int theSecond = 0;
t.GetTime(ref theHour, ref theMinute, ref theSecond);
Console.WriteLine("Current time: {0}:{1}:{2}", theHour, theMinute, theSecond);
}
}
}
Em có 2 đoạn code như trên. Mọi người cho em hỏi là tại sao đoạn code thứ 2 phải cần tham chiếu ref thì mới có thể lấy giá trị tham số của phương thức GetTime trong class class1 còn đoạn code thứ nhất thì không cần tham chiến ref nhưng vẫn có thể lấy được giá trị tham số của phương thức Triple trong class teaster [IMG]images/smilies/icon_question.gif[/IMG][IMG]images/smilies/icon_question.gif[/IMG][IMG]images/smilies/icon_question.gif[/IMG][IMG]images/smilies/icon_question.gif[/IMG]
View more random threads:
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...