Skip to content

Commit 0580609

Browse files
committed
feat: add comprehensive code examples in Kotlin, Rust, and C#
- Added code examples covering variables, functions, control flow, classes, enums, generics, error handling, and more in Kotlin, Rust, and C#.
1 parent f73a13f commit 0580609

File tree

3 files changed

+466
-0
lines changed

3 files changed

+466
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace ComprehensiveCSharpExample
6+
{
7+
// 1. 枚举
8+
enum Direction { North, South, East, West }
9+
10+
// 2. 接口
11+
interface IDrawable
12+
{
13+
void Draw();
14+
}
15+
16+
// 3. 结构体
17+
struct Point
18+
{
19+
public int X { get; }
20+
public int Y { get; }
21+
22+
public Point(int x, int y)
23+
{
24+
X = x;
25+
Y = y;
26+
}
27+
}
28+
29+
// 4. 类与继承、多态
30+
class Shape
31+
{
32+
public virtual double Area() => 0.0;
33+
}
34+
35+
class Circle : Shape
36+
{
37+
public double Radius { get; }
38+
39+
public Circle(double radius)
40+
{
41+
Radius = radius;
42+
}
43+
44+
public override double Area() => Math.PI * Radius * Radius;
45+
}
46+
47+
class Square : Shape, IDrawable
48+
{
49+
public double Side { get; }
50+
51+
public Square(double side)
52+
{
53+
Side = side;
54+
}
55+
56+
public override double Area() => Side * Side;
57+
58+
public void Draw()
59+
{
60+
Console.WriteLine($"Drawing a square with side {Side}");
61+
}
62+
}
63+
64+
// 5. 泛型类
65+
class Box<T>
66+
{
67+
public T Value { get; }
68+
69+
public Box(T value)
70+
{
71+
Value = value;
72+
}
73+
}
74+
75+
class Program
76+
{
77+
// 6. 委托与事件
78+
public delegate void Notify(string message);
79+
public static event Notify OnNotify;
80+
81+
static void Main(string[] args)
82+
{
83+
// 7. 变量与数据类型
84+
int number = 42;
85+
double pi = 3.14159;
86+
bool isTrue = true;
87+
char letter = 'C';
88+
string text = "Hello, C#!";
89+
90+
Console.WriteLine($"Number: {number}, Pi: {pi}, IsTrue: {isTrue}, Letter: {letter}, Text: {text}");
91+
92+
// 8. 控制流
93+
if (number > 40)
94+
{
95+
Console.WriteLine("Number is greater than 40");
96+
}
97+
else
98+
{
99+
Console.WriteLine("Number is 40 or less");
100+
}
101+
102+
for (int i = 0; i < 5; i++)
103+
{
104+
Console.WriteLine($"i = {i}");
105+
}
106+
107+
int counter = 0;
108+
while (counter < 5)
109+
{
110+
Console.WriteLine($"Counter = {counter}");
111+
counter++;
112+
}
113+
114+
// 9. 函数调用
115+
int sum = Add(10, 20);
116+
Console.WriteLine($"Sum: {sum}");
117+
118+
// 10. 枚举使用
119+
Navigate(Direction.East);
120+
121+
// 11. 类与对象
122+
Circle circle = new Circle(5.0);
123+
Square square = new Square(4.0);
124+
Console.WriteLine($"Circle Area: {circle.Area()}");
125+
Console.WriteLine($"Square Area: {square.Area()}");
126+
square.Draw();
127+
128+
// 12. 结构体
129+
Point point = new Point(10, 20);
130+
Console.WriteLine($"Point coordinates: X = {point.X}, Y = {point.Y}");
131+
132+
// 13. 泛型
133+
Box<int> intBox = new Box<int>(123);
134+
Box<string> stringBox = new Box<string>("Generics in C#");
135+
Console.WriteLine($"Int Box: {intBox.Value}");
136+
Console.WriteLine($"String Box: {stringBox.Value}");
137+
138+
// 14. 集合与 LINQ
139+
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
140+
var doubled = numbers.Select(n => n * 2);
141+
var filtered = numbers.Where(n => n > 3);
142+
143+
Console.WriteLine("Doubled numbers: " + string.Join(", ", doubled));
144+
Console.WriteLine("Filtered numbers: " + string.Join(", ", filtered));
145+
146+
// 15. 异常处理
147+
try
148+
{
149+
int result = Divide(10, 0);
150+
Console.WriteLine($"Result: {result}");
151+
}
152+
catch (DivideByZeroException e)
153+
{
154+
Console.WriteLine("Error: " + e.Message);
155+
}
156+
157+
// 16. 事件触发
158+
OnNotify += MessageHandler;
159+
OnNotify?.Invoke("This is a notification message.");
160+
}
161+
162+
// 17. 函数
163+
static int Add(int a, int b) => a + b;
164+
165+
// 18. 异常处理函数
166+
static int Divide(int a, int b)
167+
{
168+
if (b == 0) throw new DivideByZeroException("Cannot divide by zero");
169+
return a / b;
170+
}
171+
172+
// 19. 枚举与 switch 语句
173+
static void Navigate(Direction direction)
174+
{
175+
switch (direction)
176+
{
177+
case Direction.North:
178+
Console.WriteLine("Going North");
179+
break;
180+
case Direction.South:
181+
Console.WriteLine("Going South");
182+
break;
183+
case Direction.East:
184+
Console.WriteLine("Going East");
185+
break;
186+
case Direction.West:
187+
Console.WriteLine("Going West");
188+
break;
189+
}
190+
}
191+
192+
// 20. 事件处理函数
193+
static void MessageHandler(string message)
194+
{
195+
Console.WriteLine($"Received message: {message}");
196+
}
197+
}
198+
}

extension-manual-tests/Sample.kt

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// 1. 变量与常量
2+
val pi: Double = 3.14159 // 常量 (不可变)
3+
var x: Int = 5 // 变量 (可变)
4+
x = 6
5+
6+
// 2. 基本数据类型与字符串模板
7+
val integer: Int = 42
8+
val float: Float = 3.14f
9+
val boolean: Boolean = true
10+
val character: Char = 'K'
11+
val string: String = "Hello, Kotlin!"
12+
println("Integer: $integer, Float: $float, Boolean: $boolean, Character: $character, String: $string")
13+
14+
// 3. 函数与默认参数
15+
fun add(a: Int, b: Int = 10): Int {
16+
return a + b
17+
}
18+
19+
val sum = add(5)
20+
println("Sum: $sum")
21+
22+
// 4. 控制流
23+
if (sum > 10) {
24+
println("Sum is greater than 10")
25+
} else {
26+
println("Sum is 10 or less")
27+
}
28+
29+
for (i in 0..4) {
30+
println("i = $i")
31+
}
32+
33+
var counter = 0
34+
while (counter < 5) {
35+
println("Counter = $counter")
36+
counter++
37+
}
38+
39+
// 5. 类与对象
40+
class Rectangle(val width: Int, val height: Int) {
41+
fun area(): Int {
42+
return width * height
43+
}
44+
}
45+
46+
val rect = Rectangle(30, 50)
47+
println("The area of the rectangle is ${rect.area()} square pixels.")
48+
49+
// 6. 继承与抽象类
50+
abstract class Shape {
51+
abstract fun area(): Double
52+
}
53+
54+
class Circle(val radius: Double) : Shape() {
55+
override fun area(): Double {
56+
return pi * radius * radius
57+
}
58+
}
59+
60+
val circle = Circle(5.0)
61+
println("The area of the circle is ${circle.area()} square units.")
62+
63+
// 7. 接口
64+
interface Drawable {
65+
fun draw()
66+
}
67+
68+
class Square(val side: Int) : Drawable {
69+
override fun draw() {
70+
println("Drawing a square with side $side")
71+
}
72+
}
73+
74+
val square = Square(10)
75+
square.draw()
76+
77+
// 8. 数据类与解构
78+
data class Point(val x: Int, val y: Int)
79+
80+
val point = Point(10, 20)
81+
val (xCoord, yCoord) = point
82+
println("Point coordinates: x = $xCoord, y = $yCoord")
83+
84+
// 9. 枚举
85+
enum class Direction {
86+
NORTH, SOUTH, EAST, WEST
87+
}
88+
89+
fun navigate(direction: Direction) {
90+
when (direction) {
91+
Direction.NORTH -> println("Going North")
92+
Direction.SOUTH -> println("Going South")
93+
Direction.EAST -> println("Going East")
94+
Direction.WEST -> println("Going West")
95+
}
96+
}
97+
98+
navigate(Direction.EAST)
99+
100+
// 10. 集合与集合操作
101+
val numbers = listOf(1, 2, 3, 4, 5)
102+
val doubled = numbers.map { it * 2 }
103+
val filtered = numbers.filter { it > 3 }
104+
println("Numbers: $numbers")
105+
println("Doubled: $doubled")
106+
println("Filtered: $filtered")
107+
108+
// 11. 异常处理
109+
fun divide(a: Int, b: Int): Int {
110+
return try {
111+
a / b
112+
} catch (e: ArithmeticException) {
113+
println("Error: Division by zero")
114+
0
115+
}
116+
}
117+
118+
val result = divide(10, 0)
119+
println("Result: $result")
120+
121+
// 12. 泛型
122+
class Box<T>(val item: T) {
123+
fun getItem(): T {
124+
return item
125+
}
126+
}
127+
128+
val intBox = Box(123)
129+
val stringBox = Box("Hello, Generics!")
130+
println("Int Box: ${intBox.getItem()}")
131+
println("String Box: ${stringBox.getItem()}")

0 commit comments

Comments
 (0)