using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
namespace CSharpClassLab
{
internal class PublicBox
{
public double length;
public double breadth;
public double height;
}
internal class EncapsulatedBox
{
private double length;
private double breadth;
private double height;
public void SetLength(double len) { length = len; }
public void SetBreadth(double bre) { breadth = bre; }
public void SetHeight(double hei) { height = hei; }
public double GetVolume() { return length * breadth * height; }
}
internal class Line
{
private double length;
public static List<string> Events = new List<string>();
public Line()
{
Events.Add("ctor:default");
}
public Line(double len)
{
length = len;
Events.Add(string.Format(CultureInfo.InvariantCulture, "ctor:{0}", len));
}
public void SetLength(double len) { length = len; }
public double GetLength() { return length; }
}
internal class TrackedFinalizer
{
public static int FinalizedCount = 0;
~TrackedFinalizer()
{
FinalizedCount++;
}
}
internal class StaticVar
{
public static int num;
public void Count()
{
num++;
}
public int GetInstanceView()
{
return num;
}
public static int GetNum()
{
return num;
}
}
internal class Program
{
[MethodImpl(MethodImplOptions.NoInlining)]
private static void CreateShortLivedFinalizable()
{
TrackedFinalizer temp = new TrackedFinalizer();
if (temp == null)
{
throw new InvalidOperationException();
}
}
private static string Esc(string s)
{
if (s == null) return "null";
StringBuilder sb = new StringBuilder("\"");
foreach (char c in s)
{
switch (c)
{
case '\\': sb.Append("\\\\"); break;
case '"': sb.Append("\\\""); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
default:
if (c < 32) sb.AppendFormat("\\u{0:x4}", (int)c);
else sb.Append(c);
break;
}
}
sb.Append("\"");
return sb.ToString();
}
private static string CaseJson(string name, string summary, string expected, string actual)
{
bool passed = string.Equals(expected, actual, StringComparison.Ordinal);
return string.Format(
"{{\"name\":{0},\"summary\":{1},\"expected\":{2},\"actual\":{3},\"passed\":{4}}}",
Esc(name), Esc(summary), Esc(expected), Esc(actual), passed ? "true" : "false"
);
}
private static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<string> cases = new List<string>();
// Case 1: Class definition & dot operator access
PublicBox b1 = new PublicBox();
b1.height = 5.0; b1.length = 6.0; b1.breadth = 7.0;
PublicBox b2 = new PublicBox();
b2.height = 10.0; b2.length = 12.0; b2.breadth = 13.0;
double v1 = b1.height * b1.length * b1.breadth;
double v2 = b2.height * b2.length * b2.breadth;
string actual1 = string.Format(CultureInfo.InvariantCulture, "v1={0},v2={1}", v1, v2);
cases.Add(CaseJson(
"class_definition_and_dot_access",
"验证 Box 类实例化与点运算符读写公有字段并计算两个盒子体积",
"v1=210,v2=1560",
actual1
));
// Case 2: Encapsulation with private fields and public methods
EncapsulatedBox eb1 = new EncapsulatedBox();
eb1.SetLength(6.0); eb1.SetBreadth(7.0); eb1.SetHeight(5.0);
EncapsulatedBox eb2 = new EncapsulatedBox();
eb2.SetLength(12.0); eb2.SetBreadth(13.0); eb2.SetHeight(10.0);
string actual2 = string.Format(
CultureInfo.InvariantCulture,
"eb1={0},eb2={1}",
eb1.GetVolume(), eb2.GetVolume()
);
cases.Add(CaseJson(
"class_encapsulation_methods",
"验证将字段设为 private 并通过公有 Set/Get 方法封装对象状态",
"eb1=210,eb2=1560",
actual2
));
// Case 3: Default constructor & parameterized constructor
Line.Events.Clear();
Line l1 = new Line();
l1.SetLength(6.0);
Line l2 = new Line(10.0);
double initL2 = l2.GetLength();
l2.SetLength(6.0);
string actual3 = string.Format(
CultureInfo.InvariantCulture,
"events={0},l1={1},l2init={2},l2after={3}",
string.Join("|", Line.Events.ToArray()), l1.GetLength(), initL2, l2.GetLength()
);
cases.Add(CaseJson(
"constructors_default_and_parameterized",
"验证无参构造函数与参数化构造函数在对象实例化时的执行与初始值设定",
"events=ctor:default|ctor:10,l1=6,l2init=10,l2after=6",
actual3
));
// Case 4: Destructor / Finalizer (~ClassName) invoked via GC
TrackedFinalizer.FinalizedCount = 0;
CreateShortLivedFinalizable();
GC.Collect();
GC.WaitForPendingFinalizers();
string actual4 = string.Format("finalized={0}", TrackedFinalizer.FinalizedCount);
cases.Add(CaseJson(
"destructor_finalizer_gc",
"验证无参无返回析构函数(终结器 ~TrackedFinalizer)在对象不可达并触发 GC 终结队列后执行",
"finalized=1",
actual4
));
// Case 5: Static fields shared across instances & static methods
StaticVar.num = 0;
StaticVar s1 = new StaticVar();
StaticVar s2 = new StaticVar();
s1.Count(); s1.Count(); s1.Count();
int afterS1 = StaticVar.GetNum();
s2.Count(); s2.Count(); s2.Count();
string actual5 = string.Format(
"afterS1={0},s1View={1},s2View={2},staticCall={3}",
afterS1, s1.GetInstanceView(), s2.GetInstanceView(), StaticVar.GetNum()
);
cases.Add(CaseJson(
"static_fields_and_methods",
"验证 static 字段在多实例间共享单一副本以及通过类名直接调用静态方法",
"afterS1=3,s1View=6,s2View=6,staticCall=6",
actual5
));
Console.WriteLine(string.Format(
"{{\"version\":{0},\"cases\":[{1}]}}",
Esc("CLR " + Environment.Version.ToString()),
string.Join(",", cases.ToArray())
));
}
}
}
本例需在 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。
入口文件:projects/csharp-class-lab/ExploreClass.cs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
ExploreClass.cs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1323.zip