By GokiSoft.com|
19:59 14/10/2021|
C Sharp
[Video] Tìm hiểu anonymous methods C#, extension methods C#, anonymous types C#, partial C# - Lập trình C Sharp - C2010L
Link Video Bài Giảng
#Program.cs
using System;
using System.Collections.Generic;
namespace Lesson06
{
delegate void ShowMsg(string msg);
class Program
{
static void Main(string[] args)
{
//Test01();
Test02();
}
static void Test02()
{
//Phan 1: Anonymous methods
ShowMsg showMsg = delegate (string msg)
{
Console.WriteLine("Hello >> " + msg);
};
showMsg("ABC");
showMsg = (string msg) =>
{
Console.WriteLine("Hello >> " + msg);
};
showMsg("ABC");
showMsg = (msg) =>
{
Console.WriteLine("Hello >> " + msg);
};
showMsg("ABC");
showMsg = (msg) => Console.WriteLine("Hello >> " + msg);
showMsg("ABC");
//Phan 2: extension methods
string s = "Sinh vien Aptech 54 Le Thanh Nghi";
int wordNum = s.CountWords();//Dem so tu trong 1 string
Console.WriteLine("So tu trong string: " + wordNum);
//Phan 3: anonymous types
var std = new Student();
std.ShowMsg();
var obj = new { fullname = "Tran Van A", age = 32, address = "Ha Noi" };
Console.WriteLine("Fullname: " + obj.fullname);
//Phan 4: Partial
Student student = new Student();
student.Email = "a@gmail.com";
student.Fullname = "ABC";
}
static void Test01()
{
List<int> list1 = new List<int>();
list1.Add(1);
list1.Add(3);
list1.Add(5);
List<string> list2 = new List<string>();
list2.Add("2312");
list2.Add("sdfsdfsdf");
list2.Add("sdf34534");
//List<int> & List<string> => May class object => 1 class object.
}
}
}
#ExtensionString.cs
using System;
namespace Lesson06
{
static public class ExtensionString
{
static public int CountWords(this string s)
{
s = s.Trim();
string[] elements = s.Split(" ");
return elements.Length;
}
}
}
#Student.cs
using System;
namespace Lesson06
{
public partial class Student
{
public string Fullname { get; set; }
public string RollNo { get; set; }
public Student()
{
}
public void ShowMsg()
{
Console.WriteLine("Testing ...");
}
}
}
#Student01.cs
using System;
namespace Lesson06
{
public partial class Student
{
public string Email { get; set; }
public Student(string fullname, string rollNo, string email)
{
Fullname = fullname;
RollNo = rollNo;
Email = email;
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)