char 转换成 string
char[] chars = new char[] { 'a','b','c','d','e','f'};
string a = new string(chars);
Console.WriteLine(a);
float 转换成 int
float fvalue;
fvalue = 100.923f;
int Ivalue = (int)fvalue;
Console.WriteLine(fvalue);
Console.WriteLine(Ivalue);
string 转换成 int
string svalue = "945";
Ivalue = 0;
Ivalue = int.Parse(svalue);
Console.WriteLine(Ivalue);
如果
int 转换成 string
使用tostring 就可以了
console.write() 输出语法 以及 string 字符串连接用法
string 和 console.wirte通用的都一样!!!!
Console.Write()内部输出字符串+变量方法:
方法一:
使用 “{0}”方法
int a = 100;
Console.WriteLine("爷爷今年{0}岁了",a);
方法二:
使用“”+方法
int a = 100;
Console.WriteLine("爷爷今年"+a+"岁了");
方法三:
使用$"{a}"方法
int a = 100;
Console.WriteLine($"爷爷今年{a}岁了");
class的构造函数:
//没有加函数的代码
public class Student
{
public string Name;
public int Id;
public void print()
{
Console.WriteLine($"名字:{Name},序号:{Id}");
}
}
static void Main(string[] args)
{
Student student = new Student();
student.Name = "奥里给";
student.Id = 250;
student.print();
}
没有使用构造函数 在初始化成员变量的非常的麻烦
这是加了构造函数的代码
public class Student
{
public string Name;
public int Id;
public void print()
{
Console.WriteLine($"名字:{Name},序号:{Id}");
}
public Student(string neme,int id)
{
this.Name = neme;
this.Id = id;
}
}
static void Main(string[] args)
{
Student student = new Student("aoligei",250);
student.print();
}
在初始化的时候就很舒服,直接实例时候就初始化了
构造函数可以有多个参数,也可以没有参数
访问修饰符
public 不管内部还外部类都可以访问
private 只允许自己内部类成员访问
static 静态属性,静态方法
public class Student
{
public static string Name = "";
public static int Id = 0;
public string name = "";
public int id = 0;
public static p1()
{
Console.WriteLine("静态成员");
}
public p2()
{
Console.WriteLine("非静态成员");
}
public void print1()
{
Console.WriteLine($"1名字:{Name},学号{Id}");
都能被调用
Name Id //静态变量
name id //非静态变量
p1();//静态函数
p2();//非静态函数
}
static public void print2()
{
Console.WriteLine($"名字:{Name},学号{Id}");
可以被调用
Name Id //静态变量
p1(); //静态函数
不能被调用
//p2(); //非静态函数
//name id //非静态变量
}
public Student()
{
//都能被调用
Name
Id //静态变量
name
id //非静态变量
p1();//静态函数
p2();//非静态函数
}
public Student(string name,int id)
{
Name = name;
Id = id;
}
}
内部:
静态成员函数 只能调用静态成员变量,函数 不能调用 非静态成员变量,函数
非静态成员函数 既能调用非静态成员变量,函数 也能调用 静态成员变量,函数(都能调用)
构造函数 既能调用非静态成员变量,函数 也能调用 静态成员变量,函数(都能调用)
外部:
在main函数里面调用函数和变量的时候,加了static修饰的函数或变量,只能被类名调用。
ref ,out 修饰符
ref修饰符号用法
static void test(ref int x) //1
{
x += 10;
}
static void Main(string[] args)
{
int x = 0;
test(ref x); //2
Console.WriteLine(x);
}
最后输出 x 为 = 10
也就是说凡是用ref修饰的变量 在传递的过程中需要先赋初始值 才能进行调用
使用方法:
1.在自定义函数的途中应该这样(ref int x)
2.在函数调用传参时候需要这样(ref x)
总结: ref是先初始化在传递参数,传递回来的是运算完毕的数据
out 修饰符号用法
static void test(out int x) //1
{
x = 0; //2
x += 10; //3
}
static void Main(string[] args)
{
int x;
test(out x); //2
Console.WriteLine(x); //4
}
最后输出x = 10
也就是说凡是用ref修饰的变量 在传递的过程中可以不用赋值
在自定义函数里面使用时候需要赋值才能进行调用。
还没有普及完!以后慢慢普及