Skip to content

Commit

Permalink
Dipose
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 20, 2020
1 parent a4a4257 commit 6bf8417
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CS023_Dispose/.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/CS023_Dispose.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 CS023_Dispose/.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}/CS023_Dispose.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/CS023_Dispose.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/CS023_Dispose.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
8 changes: 8 additions & 0 deletions CS023_Dispose/CS023_Dispose.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

namespace CS023_Dispose {
class A : IDisposable {
bool resource = true;
public void Dispose () {
Console.WriteLine ("Phương thức này gọi tự động khi hết using");
resource = false; // giải phóng tài nguyên
}
}




class Program {
static void Main (string[] args) {

// using (var a = new A ()) {
// Console.WriteLine ("Do something ...");
// }

}
}
}
46 changes: 46 additions & 0 deletions CS023_Dispose/WriteData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;

namespace CS023_Dispose {

public class WriteData : IDisposable {

// trường lưu trạng thái Dispose
private bool m_Disposed = false;

private StreamWriter stream;

public WriteData (string filename) {
stream = new StreamWriter (filename, true);
}

// Phương thức triển khai từ giao diện
public void Dispose () {
Dispose (true);
GC.SuppressFinalize (this);
}

// Nếu disposing = true -> Được thi hành do gọi trực tiếp (do Dispose gọi)
// tài nguyên managed, unmanaged được giải phóng
// nếu disposing = fale -> Được thi hành bởi phương thức hủy, chỉ cần giải phóng
// các toàn nguyên unmanaged.
protected virtual void Dispose (bool disposing) {
if (!m_Disposed) {
if (disposing) {
// các đối tượng có Dispose gọi ở đây
stream.Dispose();
}

// giải phóng các tài nguyên không quản lý được cửa lớp (unmanaged)

m_Disposed = true;
}
}

~WriteData () {
Dispose(false);
}

}

}

0 comments on commit 6bf8417

Please sign in to comment.