1.命名:
变量用驼峰法;
其他命名空间、类、函数等用Pascal方法;
2变量类型
int(32bit)long(64bit)
int x = 2;
long y = 3L;
float z = 3.0F;
double m = 4.0D;
char c = 'c';
string str = "123";
bool b = true;
//行注释;
/*块注释
*/
提示:ctrl+k+d可以快速整理杂乱的代码;
快速注释代码:选中然后ctrl+KC;取消注释:ctrl+KU;
var 声明隐式类型变量;(C#是强类型语言,变量的类型一旦确定就不能改变)
常量的定义:后面必须跟着初始化器:
const int x = 100;
3.一些例程:
var x = 4;
Console.WriteLine(x.GetType().Name);
输出结果:Int32
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassAndInstance3
{
class Program
{
static void Main(string[] args)
{
Calculator c = new Calculator();
int x = c.Add(3, 4);
Console.WriteLine(x);//cw+两下tab即可快速打出
}
class Calculator
{
public int Add(int a, int b)
{
int result = a + b;
return result;
}
}
}
}
输出结果为7.
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassAndInstance3
{
class Program
{
static void Main(string[] args)
{
Day d = new Day();
string sayDay = d.GetDay();
Console.WriteLine(sayDay);
}
class Day
{
public string GetDay()
{
int day = DateTime.Now.Day;
return day.ToString();
}
}
}
}
输出:5
正确
4.使用performance monitor查看进程的堆内存使用量。
5.可以使用如下方法看不同数字变量的最大最小值范围:
uint x = uint.MaxValue;
Console.WriteLine(x);
sbyte y = sbyte.MinValue;
Console.WriteLine(y);
sbyte z = sbyte.MaxValue;
Console.WriteLine(z);
输出结果:
4294967295
-128
127
还可以:
string binStr = Convert.ToString(y, 2);
Console.WriteLine(binStr);
输出:1111111110000000
含义:转成2进制表示;