方法
using System;
namespace ConsoleApp1 {
class Program {
static void Main(string[] args) {
Program program = new Program();
program.MyMethod();
program.MyMethod2();
program.MyMethod3(1,"123");
int a = 50;
program.MyMethod4(ref a);
print(a);
int b;
program.MyMethod5(out b);
print(b);
Console.ReadKey();
}
public static void print(object obj) {
Console.WriteLine(obj);
}
public void MyMethod() {
Console.WriteLine("我是一个自定义方法");
}
public int MyMethod2() {
return 20;
}
public void MyMethod3(int a,string b) {
Console.WriteLine(a);
Console.WriteLine(b);
}
public void MyMethod4(ref int a) {
a += 10;
}
public void MyMethod5(out int a) {
a = 10;
a -= 5;
}
}
}
空
using System;
namespace ConsoleApp2 {
class Program {
static void Main(string[] args) {
//第一种方式
Nullable<short> na = null;
//第二种方式
int? a = null;
//双问号的使用
int c = a ?? 10;
print(a);
print(c);
}
public static void print(object obj) {
Console.WriteLine(obj);
}
}
}
数组
int[] arr = new int[2];
arr[0] = 1;
arr[1] = 1;
int[] arr1 = new int[3] { 1, 2, 3 };
int[] arr2 = new int[] { 1, 2, 3 };
int[] arr3 = { 1, 2, 3 };
string
string a = "hello world";
string b = "hello";
//a包含b
print(a.Contains(b));
//a相同b
print(a.Equals(b));
//a以b开头
print(a.StartsWith(b));
//a以b结束
print(a.EndsWith(b));
//返回下标
print(a.IndexOf("wo"));
//格式替换
string c = "hello{0} world{1}";
print(string.Format(c,"你好","世界"));
类