快捷键
占位符
Console.WriteLine("第一个数字是{0},第二个数字是{1},第三个数字是{3}", n1, n2, n3);
@
@能取消斜线的转义作用,将字符串按照编辑的原格式输出
string str = "11\n11";
System.IO.File.WriteAllText(@"C:\Users\xiaomi\Desktop\123.txt",str);
Console.WriteLine("写入成功");
Console.ReadKey();
一维数组的定义形式
元素类型[] 数组名称 = new 元素类型[数组长度];
int[] a = new int[4];
string[] month;
month = new string[12];
数组的索引是从0开始
二维数组
int[,] A = new int[3,3];
int[,,] B = new int[3, 2, 4];
int[,] A = new int[, ] { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
int[,] A = new int[,] { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } };
int x;
x = A[0, 1];
A[2, 2] = 10;
给数组赋值
using System;
namespace practice
{
class Program
{
static void Main(string[] args)
{
int[,] A = new int[3, 4];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
Console.WriteLine("请输入第{0}行,第{1}列的值",i + 1, j + 1);
A[i, j] = int.Parse(Console.ReadLine());
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("{0} ",A[i,j]);
}
//每一行输入完成后需要强制换行,输出矩阵
Console.WriteLine();
}
Console.ReadKey();
}
}
}
实现矩阵的转置
using System;
namespace practice
{
class Program
{
static void Main(string[] args)
{
int[,] A = new int[3, 4] { { 1,1,1,1},{ 2,2,2,2},{ 3,3,3,3} };
int[,] B = new int[4, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
B[j, i] = A[i, j];
}
}
//输出数组A
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0} ",A[i,j]);
}
//每一行输入完成后需要强制换行,sh
Console.WriteLine();
}
//输出数组B
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("{0} ", B[i, j]);
}
//每一行输入完成后需要强制换行,sh
Console.WriteLine();
}
Console.ReadKey();
}
}
}
实现矩阵的相乘
using System;
namespace practice
{
class Program
{
static void Main(string[] args)
{
int[,] A = new int[3, 2] { { 1,1},{ 2,2},{ 3,3} };
int[,] B = new int[2, 4] { { 1,2,3,4},{ 5,6,7,8 } };
int[,] C = new int[3, 4];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 2; k++)
{
C[i, j] += A[i, k] * B[k, j];
}
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write("{0,5}", C[i, j]);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
+的作用
1)当+号两边只要有一边为字符串时,起到连接作用
2)当没有字符串时,起到相加作用
string name = "李四";
Console.WriteLine("你好" + name);
Console.ReadKey();
操作符
new
var关键字:声明隐式类型的变量,根据赋值自己判断类型。(C#是强类型语言,一旦一个变量的类型确定后,不能再转换类型了。)
int x; //显式
var y = 100D;//隐式
Console.WriteLine(y.GetType().Name);
输出为Double型
new操作符是创建一个类型的实例,并且立刻调用实例的实例构造器,
在new操作符左边有赋值符号时,在变量和实例之间构造了关系,把实例的地址交给负责访问实例的变量。
匿名类型和非匿名类型:
非匿名类型有自己的名字,针对非匿名类型来说,new后面必须要接类型
Form myForm = new Form() { Text = "Hello"};
对于匿名类型来说,new后面不一定要接类型,此时就用var关键字
var person = new { Name = "mmmm", Age = 34 };
var关键字是一个偷懒的方法,我们鼓励用var来声明对象
new操作符很强大,但是我们在使用new时需要谨慎
checked,unchecked
检查一个值在内存中有无溢出,C#是一个强类型语言,任何一个变量在内存中都是有数据类型的,一个值所占数据空间的大小,决定了这个值能够表达的范围。checked检查有无溢出,unchecked表示不用检查溢出。C#默认采用unchecked模式
sizeof
获取一个对象在内存中所占字节数的尺寸
默认只能获取基本数据类型
->
在c和c++中访问对象成员的操作符,C#中有真正的指针,在使用->时,是直接针对内存的,所以必须放在不安全的上下文中使用。
通过点访问都是直接访问,通过pStu->都是间接访问
using System;
namespace instanceand
{
class Program
{
static void Main(string[] args)
{
unsafe
{
Student stu;
stu.ID = 1;
stu.Score = 99;
Student* pStu = &stu;
pStu->Score = 100;
Console.WriteLine(stu.Score);
}
}
}
struct Student
{
public int ID;
public long Score;
}
}
&(取地址操作符),*(引用操作符)
pStu->Score = 100;
(*pStu).Score = 1000;
引用操作符和取地址操作符是一元操作符,优先级比点操作符低,所以需要打括号
类型转换
- 隐式转换
- 显式转换
- 自定义转换
隐式转换
不丢失精度的隐式转换
子类向父类的隐式类型转换
using System;
namespace instanceand
{
class Program
{
static void Main(string[] args)
{
Teacher t = new Teacher();
Human h = t; //子类向父类的隐式类型转换
}
}
class Animal //创建子类
{
public void Eat()
{
Console.WriteLine("eating");
}
}
class Human:Animal
{
public void Think()
{
Console.WriteLine("who am I");
}
}
class Teacher:Human
{
public void Teach()
{
Console.WriteLine("Teach");
}
}
}
显示转换
使用convert类能够进行任意类到任意类的转换
运算
浮点除法
double x = 5.0;
double y = 4.0;
double z = x / y;
Console.WriteLine(z);
注意类型提升