Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    cho em hỏi một chút về tham chiếu ref ^^

    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]

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Không cần tham chiếu nhưng nó có return trả về cơ mà bạn.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •