创作人 Leo
编辑时间 Fri Jul 29,2022 at 19:47
Visual Studio
VS code
新建 test.cs 文件
using System;
namespace TestNS
{
class TestClass
{
static void Main(string[] args)
{
Console.WriteLine("hello");
}
}
}
在终端执行 mono 命令
$ csc test.cs
Microsoft (R) Visual C# Compiler version 3.6.0-4.20224.5 (ec77c100)
Copyright (C) Microsoft Corporation. All rights reserved.
$ mono test.exe
hello
参考:
c# 官方文档
c#.net 官方 APi 文档
菜鸟 c#
创建数组以及 C# 程序的基本结构
using System; // 引用一个包
// namespace 声明名字空间
namespace TestNS
{
// 定义一个类
class TestClass
{
// 主入口函数,程序从这里开始执行;这个入口接口类似 Java
static void Main(string[] args)
{
Console.WriteLine("hello");
string[] testStrs = new string[2]{"hello", "world"};
testStrs[1] = "apple";
for (int i=0; i<testStrs.Length; i++)
{
Console.WriteLine(string.Format("content of {0} is {1}", i, testStrs[i]));
}
}
}
}
数组是静态的,长度固定的,如果需要实现其他语言动态数组,可以使用内置类 ArrayList
循环遍历数组
for 循环
foreach 循环
static void ForeachTest()
{
string[] ss = new string[]{
"a","b","c","d","e"
};
foreach (string s in ss)
{
Console.WriteLine(s);
}
}
强制类型转换
var n = (int)100;
base64 编码
String 转 Byte 数组
// Convert: using System;
// Encoding: using System.Text;
string result = Convert.ToBase64String(Encoding.ASCII.GetBytes("hello world")) ;
命名空间 namespace 用来防止不同程序包类重名问题
通过 class 定义类
System.Object 是所有 c sharp 类的基础类
类成员默认为 private,通过 public 指定为公有成员
static 标识成员为静态的,即不属于任何实例化的类,在内存中仅存在一份,直接通过 类名.成员 即可访问
const 成员常数,常数就是静态的,而且是不能改变的,所以不需要再用 static 标识
function 声明一个成员方法
ref 声明一个参数是引用的
out 声明一个参数是输出参数
跟类名同名的函数是构造函数,标记了 static 则为静态构造函数
委托可以实现其他语言函数指针的功能,但是它不同于函数指针,通过 delegate 关键字声明一个委托
delegate int Fn(int a, int b)
Programming.log - a place to keep my thoughts on programming
格式化输出时间
static void DateTest()
{
DateTime dt = DateTime.Now;
string s = string.Format("{0:yyyy-MM-dd_HH:mm:ss}", dt);
Console.WriteLine(s);
}
NuGet 是 Visual Studio 中的包管理器
参考: Unity3D 入门:为 Unity 的 C# 项目添加 dll 引用或安装 NuGet 包 Unity3D 入门:为 Unity 的 C# 项目添加 dll 引用或安装 NuGet 包
System.IO.Compression DeflateStream 函数书 raw 格式;没有 2 字节头,和 4 字节尾;不能兼容 golang 等其他语言
推荐使用第三方库 SharpZipLib