By GokiSoft.com| 08:53 13/10/2021|
C Sharp

[Video] Tìm hiểu Extension method C#, Partial C#, Type Anonymous C#


Link Video Bai Hoc

#Student01.cs


using System;
namespace Lesson07
{
    public partial class Student
    {
        public string Rollno { get; set; }
        public string Email { get; set; }

        public Student(string fullname, string address, string rollno, string email)
        {
            Fullname = fullname;
            Address = address;
            Rollno = rollno;
            Email = email;
        }

        public void Display()
        {
            Console.WriteLine(Fullname);
            Console.WriteLine(Email);
            Console.WriteLine(Address);
            Console.WriteLine(Rollno);
        }
    }
}


#Student.cs


using System;
namespace Lesson07
{
    public partial class Student
    {
        public string Fullname { get; set; }
        public string Address { get; set; }

        public Student()
        {
        }
    }
}


#Program.cs


using System;

namespace Lesson07
{
    delegate void ShowMsg(string msg);

    class Program
    {
        static void Main(string[] args)
        {
            //Anonymous method.
            ShowMsg showMsg = delegate (string msg)
            {
                Console.WriteLine("Hello > " + msg);
            };
            showMsg("123");

            showMsg = (string msg) =>
            {
                Console.WriteLine("Hello > " + msg);
            };
            showMsg("123");

            showMsg = (msg) =>
            {
                Console.WriteLine("Hello > " + msg);
            };
            showMsg("123");

            showMsg = (msg) => Console.WriteLine("Hello > " + msg);
            showMsg("123");

            //Extension method
            string s = "Sinh vien Aptech 54 Le Thanh Nghi";
            int count = s.CountWords();

            Console.WriteLine("So tu: {0}", count);

            //Partial
            Student std = new Student();
            std.Fullname = "ABC";
            std.Rollno = "R001";

            Console.WriteLine("Rollno: " + std.Rollno);

            //Type anonymos
            var obj = new { Fullname = "Tran Van A", Age = 32 };
            Console.WriteLine("Age: " + obj.Age);
            Console.WriteLine("Fullname: " + obj.Fullname);

            string s1 = null;
            DateTime? dateTime = null;//Dart/Flutter -> Cross platform (Android + iOS)
        }
    }
}


#ExtensionString.cs


using System;
namespace Lesson07
{
    static public class ExtensionString
    {
        public static int CountWords(this string str)
        {
            str = str.Trim();
            string[] elements = str.Split(" ");
            return elements.Length;
        }
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)