Skip to content

Commit

Permalink
CS007A - Tham chiếu, tham trị
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Aug 10, 2020
1 parent a4a4257 commit 53cb67a
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CS007A-REF-VALUE/.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/CS007A-REF-VALUE.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 CS007A-REF-VALUE/.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}/CS007A-REF-VALUE.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/CS007A-REF-VALUE.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/CS007A-REF-VALUE.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
9 changes: 9 additions & 0 deletions CS007A-REF-VALUE/CS007A-REF-VALUE.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>CS007A_REF_VALUE</RootNamespace>
</PropertyGroup>

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

namespace CS007A_REF_VALUE
{

class Stutent
{
public string Name;
public Stutent(string name)
{
this.Name = name;
}
}




class Program
{
public static void valuetypeExample()
{
// biến a, kiểu int là kiểu giá trị, nó lưu giá trị 100
int a = 100;
// kiểu giá trị khi gán, giá trị được copy
int b = a;
Console.WriteLine($"a = {a}, b = {b}");

// b gán giá trị mới, a không thay đổi - vì chúng không cùng tham
// chiếu đến 1 đối tượng chung, mà mỗi biến lưu một giá trị
b = 200;
Console.WriteLine($"a = {a}, b = {b}");
// a = 100, b = 100
// a = 100, b = 200
}

// Tham số kiểu lớp, nên là tham chiếu
static void UpperName(Stutent stutent)
{
stutent.Name = stutent.Name.ToUpper();
}

public static void ThamSoThamChieu(ref int x) {
x = x * x;
Console.WriteLine(x);
}

static void Main(string[] args)
{
Stutent a = new Stutent("Nguyễn Văn A");
Console.WriteLine(a.Name); // Nguyễn Văn A

Stutent b;
// Khi gán, tham chiếu không sao chép giá trị mà tham chiếu địa chỉ
// nên có thể có a là b, b là a, b thay đổi nghĩa là a thay đổi
b = a;

b.Name = "Nguyễn Văn B";
Console.WriteLine(a.Name); // Nguyễn Văn B

Stutent stutent1 = new Stutent("Xuan Thu Lab");
UpperName(stutent1); // Phương thức biến đổi tham số student, có nghĩa là
// biến đổi biến student1

Console.WriteLine(stutent1.Name); // XUAN THU LAB

}


}
}

0 comments on commit 53cb67a

Please sign in to comment.