Skip to content

Commit

Permalink
Delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 11, 2020
1 parent a4a4257 commit e5c6ced
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CS008_Anonymous/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/CS008_Anonymous.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
42 changes: 42 additions & 0 deletions CS008_Anonymous/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/CS008_Anonymous.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/CS008_Anonymous.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/CS008_Anonymous.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
9 changes: 9 additions & 0 deletions CS008_Anonymous/CS008_Anonymous.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>CS008_Anonymous</RootNamespace>
</PropertyGroup>

</Project>
49 changes: 49 additions & 0 deletions CS008_Anonymous/FuncAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;

namespace CS008_Anonymous
{
class FuncAction
{
static int Sum(int x, int y)
{
return x + y;
}

// Sử dụng Func
public static void TestFunc(int x, int y)
{
Func<int,int,int> tinhtong; // biến tinhtong phù hợp với các hàm trả về kiểu int, có 2 tham số kiểu int
tinhtong = Sum; // Hàm Sum phù hợp nên có thể gán cho biến

var ketqua = tinhtong(x, y);
Console.WriteLine(ketqua);
}

// Sử dụng Action
public static void TestAction(string s)
{
Action<string> showLog = null;

showLog += Logs.Warning; // Nối thêm Warning vào delegate
showLog += Logs.Info; // Nối thêm Info vào delegate
showLog += Logs.Warning; // Nối thêm Warning vào delegate

// Một lần gọi thi hành tất cả các phương thức trong chuỗi delegate
showLog("TestLog");
}

// Sử dụng Delegate làm tham số phương thức, truyền callback
static void TinhTong(int a, int b, Action<string> callback)
{
int c = a + b;
// Gọi callback
callback(c.ToString());
}

public static void TestTinhTong()
{
TinhTong(5,6, (x) => Console.WriteLine($"Tổng hai số là: {x}"));
TinhTong(1,3, Logs.Info);
}
}
}
63 changes: 63 additions & 0 deletions CS008_Anonymous/Logs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;

namespace CS008_Anonymous
{
public class Logs
{
// Khai báo một delegate
public delegate void ShowLog(string message);

// Phương thức tương đồng với ShowLog (tham số, kiểu trả về)
static public void Info(string s)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(string.Format("Info: {0}", s));
Console.ResetColor();
}

// Phương thức tương đồng với ShowLog (tham số, kiểu trả về)
static public void Warning(string s)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(string.Format("Waring: {0}", s));
Console.ResetColor();
}

public static void TestShowLog()
{
ShowLog showLog;

showLog = Info; // showLog gán bằng phương thức Info
showLog("Thông báo"); // Thi hành delegate chính là thi hành Info

showLog = Warning; // showLog gán bằng phương thức Warning
showLog("Thông báo"); // Thi hành delegate chính là thi hành Info
}

// Gán nhiều hàm vào Delegate tạo thành chuỗi
public static void TestShowLogMulti()
{
ShowLog showLog;

showLog = null;
showLog += Warning; // Nối thêm Warning vào delegate
showLog += Info; // Nối thêm Info vào delegate
showLog += Warning; // Nối thêm Warning vào delegate

//Một lần gọi thi hành tất cả các phương thức trong chuỗi delegate
showLog("TestLog"); //Hoặc an toàn: showLog?.Invoke("TestLog");
}

// Cộng nhiều Delegate
public static void TestShowLogPlus()
{
ShowLog showLog1 = (x)=> {Console.WriteLine($"-----{x}-----");};
ShowLog showLog2 = Warning;
ShowLog showLog3 = Info;

var all = showLog1 + showLog2 + showLog3 + showLog1;

all("Xin Chào");
}
}
}
20 changes: 20 additions & 0 deletions CS008_Anonymous/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace CS008_Anonymous
{
class Program
{

static void Main(string[] args)
{
// Logs.TestShowLog();
// Logs.TestShowLogMulti();
// Logs.TestShowLogPlus();

// FuncAction.TestFunc(5, 6); // In ra: 11

FuncAction.TestTinhTong();
}

}
}
3 changes: 3 additions & 0 deletions CS008_Anonymous/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sử dụng Delegage, Func, Action

https://xuanthulab.net/su-dung-delegate-trong-c-ham-uy-quyen.html

0 comments on commit e5c6ced

Please sign in to comment.