By GokiSoft.com|
09:39 21/10/2021|
C Sharp
[Video] Tìm hiểu generic C#, Anonymous method C#, extension method C#, anonymous type C#, partial type C#, nullable type C# - C2009G
LINK VIDEO BAI GIANG
[Share Code] Tìm hiểu generic C#, Anonymous method C#, extension method C#, anonymous type C#, partial type C#, nullable type C# - C2009G
#Program.cs
using System;
using System.Collections.Generic;
namespace Lesson07
{
class Program
{
static void Main(string[] args)
{
//Generic
//Test01();
//Anonymous method, extension method, anonymous type, partial type, nullable type
Test02();
}
interface IRunning
{
void OnRunning();
}
class People : IRunning
{
public void OnRunning()
{
Console.WriteLine("Test interface ...");
}
}
delegate void OnRunning();
static void Test02()
{
//Phan 1: Anonymous method
People people = new People();
people.OnRunning();
//Lam sao de tao dc anonymous -> method anonymous.
OnRunning running = delegate ()
{
Console.WriteLine("Test delegate interface ...");
};
running();
running = () =>
{
Console.WriteLine("Test delegate interface ...");
};
running();
running = () => Console.WriteLine("Test delegate interface ...");
running();
//Phan 2: Extension method
string s = "Sinh vien Aptech 54 Le Thanh Nghi";
int length = s.Length;
Console.WriteLine("So ky tu: " + length);
int words = s.CountWords();
Console.WriteLine("So tu: " + words);
//Phan 3: Anonymous type
Student std = new Student();
std.ShowInfo();
var student = new Student();
student.ShowInfo();
var p = new {fullname="Tran Van A", age=22, gender="Nam"};
Console.WriteLine("fullname: " + p.fullname);
//Phan 4: Partial
Teacher teacher = new Teacher();
teacher.Input();
teacher.Display();
//Phan 5: nullable
DateTime? dateTime = null;
dateTime = DateTime.Now;
Char? c = null;
c = 'A';
Char c2;
Teacher t = null;
t = new Teacher();
Abc? abc = null;
abc = new Abc();
abc?.ShowInfo();
}
static void Test01()
{
List<int> list1 = new List<int>();
list1.Add(12);
list1.Add(32);
list1.Add(55);
//B2. Tai sao list1.Add("2dasdas") => Error
List<string> list2 = new List<string>();
list2.Add("2dasdas");
list2.Add("sdsad232");
list2.Add("sddsfsdfds");
//B2. Tai sao list2.Add(55) => Error
//B2. Check thi 2 ham nay la 1.
//B1. Cau hoi 1: List<int> & List<string> => Tong bao nhieu class object => chi 1 class object
}
}
//Extension method
static class ExtensionString
{
public static int CountWords(this string str)
{
str = str.Trim();
string[] elements = str.Split(" ");
return elements.Length;
}
}
class Student
{
public void ShowInfo()
{
Console.WriteLine("testing ...");
}
}
public readonly struct Abc
{
public void ShowInfo()
{
Console.WriteLine("testing ...");
}
}
}
#Teacher.cs
using System;
namespace Lesson07
{
public partial class Teacher
{
public string Fullname { get; set; }
public string Gender { get; set; }
public Teacher()
{
}
public void Display()
{
Console.WriteLine("Hien thi thong tin du lieu");
}
}
}
#Teacher02.cs
using System;
namespace Lesson07
{
public partial class Teacher
{
public string Email { get; set; }
public int Exp { get; set; }
public void Input()
{
Console.WriteLine("Nhap du lieu");
Console.WriteLine("Nhap ten: ");
Fullname = Console.ReadLine();
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)