using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace CSharpQuizLab
{
internal interface INamedEntity
{
string Label { get; set; }
}
internal class BaseEntity
{
public string Kind { get { return "Base"; } }
}
internal class DerivedEntity : BaseEntity, INamedEntity, IDisposable
{
public string Label { get; set; }
public bool Disposed { get; private set; }
public void Dispose()
{
Disposed = true;
}
}
internal class Program
{
private const int MaxLimit = 100;
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"
);
}
public static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<string> cases = new List<string>();
// Case 1: Primitive sizes, ordering, defaults (Q1, Q2, Q4, Q5, Q7, Q8, Q9, Q10)
int szSbyte = sizeof(sbyte);
int szShort = sizeof(short);
int szInt = sizeof(int);
int szLong = sizeof(long);
int szFloat = sizeof(float);
int charBits = sizeof(char) * 8;
bool sizeOrder = (szSbyte < szShort && szShort < szInt && szInt < szLong);
bool fitsSbyte = (35 >= sbyte.MinValue && 35 <= sbyte.MaxValue);
string defaultLitType = (35).GetType().Name;
bool defaultBool = default(bool);
string actual1 = string.Format(
"long={0};float={1};charBits={2};order={3};sbyte35={4};lit={5};boolDef={6}",
szLong, szFloat, charBits, sizeOrder, fitsSbyte, defaultLitType, defaultBool
);
cases.Add(CaseJson(
"quiz_types_sizes_defaults",
"Verify primitive sizes (long=8, float=4, char=16-bit), ordering sbyte<short<int<long, int literal default, and bool default False",
"long=8;float=4;charBits=16;order=True;sbyte35=True;lit=Int32;boolDef=False",
actual1
));
// Case 2: Declarations, Convert.ToInt32 vs cast, const, var, string/String alias (Q3, Q6, Q15, Q16, Q17)
int a = 32;
int b = 40;
float fb = 3.5f;
int convSum = 12 + Convert.ToInt32(fb); // 12 + 4 (banker's rounding to even) = 16
int castSum = 12 + (int)fb; // 12 + 3 (truncation) = 15
var inferred = "csharp";
bool stringSameAsSystemString = (typeof(string) == typeof(System.String));
string actual2 = string.Format(
"ab={0}/{1};conv={2};cast={3};const={4};varType={5};strAlias={6}",
a, b, convSum, castSum, MaxLimit, inferred.GetType().Name, stringSameAsSystemString
);
cases.Add(CaseJson(
"quiz_declarations_and_conversions",
"Verify valid variable declarations, Convert.ToInt32(3.5f) vs (int)3.5f, const, var, and string == System.String",
"ab=32/40;conv=16;cast=15;const=100;varType=String;strAlias=True",
actual2
));
// Case 3: Entry point Main, Console IO APIs, string + concat, comments (Q11, Q12, Q13, Q14, Q28, Q29)
MethodInfo entry = typeof(Program).GetMethod("Main", BindingFlags.Public | BindingFlags.Static);
bool hasWriteLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }) != null;
bool hasReadLine = typeof(Console).GetMethod("ReadLine", Type.EmptyTypes) != null;
string joined = "C#" + " " + "Quiz"; // single-line comment check
/* multi-line comment check */
string actual3 = string.Format(
"entry={0};wl={1};rl={2};concat={3}",
entry != null ? entry.Name : "", hasWriteLine, hasReadLine, joined
);
cases.Add(CaseJson(
"quiz_entry_io_strings_comments",
"Verify Main entry signature, Console.WriteLine/ReadLine reflection, + string concatenation, and comment syntax",
"entry=Main;wl=True;rl=True;concat=C# Quiz",
actual3
));
// Case 4: OOP class/interface/namespace/:/get-set/IDisposable (Q18, Q19, Q20, Q21, Q22, Q26, Q27)
DerivedEntity ent;
using (ent = new DerivedEntity { Label = "demo" })
{
if (ent.Label != "demo") throw new InvalidOperationException();
}
bool oopOk = (ent is BaseEntity) && (ent is INamedEntity) && (ent is IDisposable) && ent.Disposed;
string ns = typeof(DerivedEntity).Namespace;
cases.Add(CaseJson(
"quiz_oop_inheritance_interface_namespace",
"Verify class/interface/namespace, colon inheritance/implementation, get/set property, and IDisposable via using",
"oop=True;ns=CSharpQuizLab;kind=Base;label=demo",
string.Format("oop={0};ns={1};kind={2};label={3}", oopOk, ns, ent.Kind, ent.Label)
));
// Case 5: Control flow (if/for/while/foreach/break) & try-catch-finally (Q23, Q24, Q25, Q30)
int sumFor = 0;
for (int i = 1; i <= 5; i++)
{
if (i == 4) break;
sumFor += i; // 1 + 2 + 3 = 6
}
int wCount = 0;
while (wCount < 2) wCount++;
int feSum = 0;
foreach (int n in new int[] { 2, 4 }) feSum += n;
string exFlow = "";
try { int zero = 0; int bad = 1 / zero; Console.Write(bad); }
catch (DivideByZeroException) { exFlow += "C"; }
finally { exFlow += "F"; }
cases.Add(CaseJson(
"quiz_control_flow_and_exceptions",
"Verify if, for+break, while, foreach loops and try-catch-finally execution flow",
"for=6;while=2;foreach=6;ex=CF",
string.Format("for={0};while={1};foreach={2};ex={3}", sumFor, wCount, feSum, exFlow)
));
Console.WriteLine(string.Format(
"{{\"version\":\"csc4/{0}\",\"cases\":[{1}]}}",
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-quiz-lab/ExploreQuiz.cs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
ExploreQuiz.cs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1351.zip