C# 与 Kotlin 对照
同一个程序分别用 C# 和 Kotlin 编写,取自本课程的课时。
字符串和模板
C# 插值字符串会在引号前加上美元符号前缀。Kotlin 模板会在普通引号字符串中使用美元符号标记,而带大括号的表达式可以处理更复杂的替换。Kotlin 允许使用顶层 main 函数;JVM 编译器会提供文件门面类。辅助 Kotlin 文件不需要自己的入口点。字符串字面量和字符字面量的区别依然熟悉,但 Console.WriteLine 在 Kotlin 中变为 println,任务输出则由锁定的驱动程序负责。
C#1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6 7static void Main(){8int ready=3;Console.WriteLine($"Ready: {ready}");9}10}Kotlin1 2fun main(){3val ready=34println("Ready: $ready")5}相等与同一性
C# 字符串相等运算符会比较内容,Kotlin 中的字符串 == 也使用结构相等。C# 可以为其他类型重载相等运算符;Kotlin 的 == 会遵循 equals,而不是导入任意 C# 运算符声明。Kotlin 的 === 用于测试引用标识。普通 Kotlin 类仍然是引用对象,除非其相等性约定另有规定;而数据类会根据构造函数属性生成相等性实现。为移植的每个模型区分所需的相等性约定。
C#1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6 7static void Main(){8string a=new string(new[]{'t','e','a'}),b=new string(new[]{'t','e','a'});Console.WriteLine(a==b);9}10}Kotlin1 2fun main(){3val a=String(charArrayOf('t','e','a'))4val b=String(charArrayOf('t','e','a'))5println(a==b)6}数组与 IntArray
C# 和 Kotlin 数组的长度都是固定的,元素槽位可以修改。Kotlin 的 IntArray 用于存储原始整数,而 Array<Int> 是一种泛型数组表示形式。C# 的 Length 在 Kotlin 中对应 size,而 last 或 lastIndex 可以用于访问末尾元素。较新的 C# 末尾索引语法并不等同于 Kotlin 的负数索引。Kotlin 仍会检查边界,因此请选择明确有效的位置,或使用合适的便捷操作。
C#1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6 7static void Main(){8int[]values={4,8,12};Console.WriteLine(values[values.Length-1]);9}10}Kotlin1 2fun main(){3val a=intArrayOf(4,8,12)4println(a.last())5}while、break 和 continue
C# 和 Kotlin 都将 break 和 continue 作为独立的循环控制操作。Kotlin 还提供带标签的跳转,用于有意控制外层循环。确保 while 循环在每条继续执行的路径上都能取得进展。单词示例会跳过空项,并在遇到 STOP 时停止,这样可以清楚地区分这两个判断,同时不会泄露数字练习的答案。Kotlin 字符串的 == 会保留源内容比较。
C#1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6 7static void Main(){8var kept=new List<string>();foreach(string word in new[]{"tea","","STOP","coffee"}){if(word=="STOP")break;if(word.Length==0)continue;kept.Add(word);}Console.WriteLine(String.Join(",",kept));9}10}Kotlin1 2fun main(){3val kept=mutableListOf<String>()4for(word in listOf("tea","","STOP","coffee")){if(word=="STOP")break;if(word.isEmpty())continue;kept.add(word)}5println(kept.joinToString(","))6}顶层函数
C# 方法通常属于某个类,并且可以使用 ref 或 out 重新绑定调用方的变量。Kotlin 支持顶层函数,普通参数无法像 C# 的 ref/out 那样重新绑定变量。迁移此类 API 时,请返回一个值或专用的结果模型。Kotlin 表达式函数体可以推断结果类型,而非 Unit 的代码块函数体需要显式返回值。扩展函数也不会向接收者类中插入虚拟成员。
C#1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6static bool IsLong(string text){return text.Length>4;}7static void Main(){8Console.WriteLine(IsLong("coffee"));9}10}Kotlin1fun isLong(text:String):Boolean = text.length>42fun main(){3println(isLong("coffee"))4}输入和字符串格式化
C# 的字符串去除首尾空白和不依赖区域设置的大小写转换,在 Kotlin 中也有对应操作;需要时,还可以使用 JVM 的区域设置感知操作。输入契约决定空白是否有意义。Kotlin 的 readlnOrNull 可以区分输入结束的情况,而锁定的驱动程序会收集不带终止换行符的行。格式化和验证是不同的职责。在本练习中,请将分作为整数处理,不要引入二进制浮点数舍入。
C#1using System;2using System.Collections.Generic;3using System.Linq;4using System.Globalization;5class Program {6 7static void Main(){8Console.WriteLine(" tea ".Trim().ToUpperInvariant());9}10}Kotlin1 2fun main(){3println(" tea ".trim().uppercase(java.util.Locale.ROOT))4}
课程大纲
10 章48 节课1 个项目48 个挑战99 道测验题常见问题
面向 C# 开发者的 Kotlin 适合谁学习?
适合已经会写 C#、想学 Kotlin 的人。每节课都从你熟悉的 C# 代码出发,展示 Kotlin 如何实现同样的事情,所以你可以跳过入门基础。
需要先会 Kotlin 吗?
不需要。课程从你的第一个 Kotlin 程序开始。你需要掌握 C# 基础:变量、条件、循环、函数和集合。
面向 C# 开发者的 Kotlin 有多少节课?
面向 C# 开发者的 Kotlin 包含 48 节互动课,你可以按自己的节奏完成。
面向 C# 开发者的 Kotlin 包含哪些内容?
本课程分为以下几章:开始学习 Kotlin, 值与决策, 集合与所有权, 迭代与转换, and more。
面向 C# 开发者的 Kotlin 免费吗?
是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。