C#基本语法
C#不考虑空格、回车。因空白可以被忽略,帮,一行有多个语句。
简单类型
类型 | ||
sbyte | ||
byte | ||
short | ||
ushort | ||
int | ||
uint | ||
long | ||
ulong | ||
float | ||
double | ||
decimal | ||
char | ||
bool | ||
string |
变量命名
第一个字符字母、下划线或@
其后可以是字母、下划线或数字
命名约定
camelCase
firstName
PascalCase
LastName
赋值表达式
一下声名很多变量,或声明时即为它赋值
int age = 4;
int xSize =4,ySize = 5;
红色:语法错误
+连接两个字符
占位符
@取消字符串中的转义作用。
char ct = '\t';是转义可以的。
类型转换
显式转换
隐式转换
两种类型兼容
目前类型大于源类型
枚举类型
enum
数组
数组类型[] 数组名 = new 数组类型[数组的长度];//这种应该默认初始0值
表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace helloWd
{
public enum IOState//检举
{
OUTPUT,
IINPUT,
ZSTATE
}
public struct Persion
{
public string name;
public int age;
public char gender;
}
class Program
{
static void Main(string[] args)
{
IOState state1 = IOState.IINPUT;
int state2 = (int)state1;
Console.WriteLine(state1);//input
Console.WriteLine(state2);//1
Persion lx;
lx.name = "liuxiang";
lx.age = 30;
lx.gender = 'm';
int[] nums = new int[10];
int[] numsValue = { 1, 2, 3 };
int[] tx = new int[4] { 1,2,3,4};
int[] txFree = new int[] { 1, 2 };
string[] strTmp = new string[10];
string str = "hello";
string str2 = " world";
int n3 = 30;
double d = n3 / 2.4;
Console.WriteLine("{0:0.00}",d);
//Console.WriteLine(str+str2+" "+n3);
Console.WriteLine("string1 {0}.string {1},string {2}",str,str2,n3);
string fruit = Console.ReadLine();
Console.WriteLine("input: {0}", fruit);
string str1 = "test string\r\n";
System.IO.File.WriteAllText(@"E:\360MoveData\Users\lqf\Desktop\log\hello.txt",str1);
Console.WriteLine("input OK\r\n");
Console.ReadLine();
}
}
}
注释
注释,是一种良好的习惯,为克服自己遗忘及方便他人看代码。
C#常用注释方法
单行注释
多行注释
C#///多用来解释类和方法
#region 要空一格
#endregegion
能否使用宏?