干货满满,记得备水!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace day_01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!!!");
Console.ReadKey();
}
}
}
不换行输出:Write()
换行输出:WriteLine()
槽占位输出:WriteLine("{0}{1}",one,two)
输入字符串:ReadLine()
输入单个字符:Read()、返回整数,整数对应字符编号
热键输入:ReadKey()
Visual Studio快捷键:https://zhuanlan.zhihu.com/p/98271375
变量:可以变化的量就是变量 说明我的变量的值不固定 是随时可以进行变化操作
@有2种意义:
int money = 100;
char a = ('A');
string a = ("hello java");
float a=1.3f;
实例:默认不加字段
常量:const
const int i = 10
只读:readonly
short a = 123 ;
int b = a ;
short a = 123 ;
byte b = (byte)a ;
int i = 100; byte b = (byte)i; //b = 100
int i = 10000; byte b = (byte)i; //b = 16(符号位变化)
double d = 2.5; int i = (int)d; //i = 2(小数位舍掉)
char c = 65; int i = c; //i = 65
类型.Parse(字符串)
Convert.ToInt32(需要转换的内容)
Convert.ToDouble(需要转换的内容)
Convert.ToString(需要转换的内容)
需要转换的内容.ToString
Array.ConvertAll<需转换类型, 转换后类型>(数组名, s => 转换后类型.Parse(s))
例如:int y = 3 * (10 - 5)
;
n += 2
相当于 n = n+2
int i = 99;
int z = i ==100?666:111;
结果:z=111
数组类型[] 数组名称
数组名称 = new 数组类型[数量]
数组类型[] 数组名称 = new 数组类型[数量]
数组[下标] = 值;
int[] i = {1,2,3};
int[] i = new int[] {1,2,3};
int[] a = {
1, 2, 3, 4};
for(int i = 0; i < a.length; i++){
Console.Write("遍历:"+a[i]);
}
int[] name = new int [n] [n];
int[, ] name = new int [n, n];
int[][] arr = {
{
2, 3}, {
1, 3}, {
0, 0}, {
0, 3}};
for(int row = 0; row < arr.length; row++) {
for(int col = 0; col < arr[row].length; col++) {
Console.WriteLine("{0} ",arr[row][col]);
}
}
int[] name = new int [n] [n] [n];
int[, ,] name = new int [n, n, n];
0
0.0
\u0000
false
null
String color;
public String color;
类名 变量名 = new 类名();
Day a = new Day();
对象.属性名=值
a.color = "黄色";
访问修饰符 返回值类型 方法名( 形参的类型 形参名称 ){方法执行内容}
对象.方法名( );
a.method();
方法名( );
method();
返回值类型 变量名 = 对象.方法名( );
String js = a.method( );
对象.方法名( 实参1,实参n );
a.method ( eee: "成功", i: 5 );
类名[] 变量名 = new 类名[长度];
for(int i = 0; i < 对象数组.length; i++){
对象数组[i].方法名();
}
公共修饰符:public
私有修饰符:private
保护修饰符:protected
内部修饰符:internal
virtual:修饰方法成员,表示虚方法。父类可以含有该类的实现,子类可以覆写该函数。
override:表示该方法为覆写了父类的方法。
Readonly:修饰字段,表示该字段为只读字段。
Const:修饰字段,表示该字段为只读字段。并且在编译时必须能够明确知道该字段的值,其值是硬编码到程序中去的,修改了该类型成员后需要重新编译才能使修改生效。
Readonly不能修饰局部变量,const可以。
abstract用来修饰抽象类,表示该类只能作为父类被用于继承,而不能进行对象实例化。抽象类可以包含抽象的成员,但这并非必须。abstract不能和new同时用。
sealed用来修饰类为密封类,阻止该类被继承。同时对一个类作abstract和sealed的修饰是没有意义的,也是被禁止的。
注意:当一个类或方法没有被任何修饰符修饰时,默认为internal。
public 子类方法名([参数列表]):base(父类方法的实参) {方法代码}
if (条件1) {
条件一成立之后执行的程序
} else if (条件2) {
条件一不成立,条件二成立之后执行的程序
} else {
两个条件都不成立时执行的程序
}
switch(a){
case 1:
Console.WriteLine("a=1");
break;
case 2:
Console.WriteLine("a=2");
break;
default:
Console.WriteLine("a!=1或2");
break;
}
while(条件) {
条件成立之后循环执行的程序
}
int i = 0;
while(i < 10) {
Console.WriteLine(++i);
}
`
do{
至少循环一次的程序
}while (条件)
int i = 0;
do{
Console.WriteLine(++i);
}while (i < 10)
for (条件变量:初始值; 循环条件; 条件变量的变化){
条件成立之后循环执行的程序
}
for (int i = 0; i < 10; i++) {
Console.WriteLine(i);
}
int[] nums = {
1, 2, 3, 4};
foreach (int num in nums) {
Console.WriteLine(num);
}
abs()
Ceiling()
Floor()
Max()
Min()
pow(x,y)
sqrt()
Cos()
Cosh()
Sin()
Sinh()
Random r = new Random()
Next(指定最大值)
NextDouble()
DateTime DT = DateTime.Now
获得当前日期、格式为 2020年1月1日:ToLongDateString()
获得当前日期、格式为 2020/1/1:ToShortDateString()
获得当前时间、格式为 17:59:23:ToLongTimeString()
获得当前时间、格式为 17:59:ToShortTimeString()
获取此实例的日期:Date
#region 折叠后的说明文字
折叠的代码
#endregion
#region 输出打印i
int i = 0;
Console.WriteLine(i);
#endregion
try {
可能出现异常的代码写这里
} catch (异常类型 异常对象){
出错后执行的代码
} finally {
资源回收代码
}
字符串.Length
string Str = new string('H',20)、构造由20个H组成的字符串
string Str = new string(new char[]{'H','e','l','l','o'})、hello
string Str=“内容”; Str.Equals("内容");返回true
Equals("内容", StringComparison.Ordinal IgnoreCase)
IndexOf()
string Str="hello"; Str.IndexOf("l"); 返回整数2
LastIndexOf()
string Str="hello"; Str.LastIndexOf("l"); 返回整数3
string Str="hello"; Str.Substring(3); 返回lo
string Str="hellojava"; Str.Substring(1,6); 返回elloja
string[] 数组名= 字符串.Split('分割符')
字符串.ToLower()
大写:字符串.ToUpper()
字符串.Trim()
字符串.TrimStart();
字符串.TrimEnd();
string Str="hello"; Str.StartsWith("he"); 返回true
string Str="hello"; Str.EndsWith("o"); 返回true
string Str="hello"; Str.Contains("ell"); 返回true
string Str ="hello!"; Str.Replace("!","?"); 返回hello?
string Str="hello"; string.IsNullOrEmpty(Str); 返回true
string Str = string.Format("10+10={0}","20")
数据类型.valueOf( 整数或字符串, 指定使用的进制数 );返回对应类型
StringBuilder sb=new StringBuilder("字符串");
sb.append("字符串",索引,字符数);
sb.AppendLine("字符串");
sb.AppendFormat("字符串");
sb.insert(插入的索引值,插入的内容);
sb.remove(开始的索引值,移除的长度);
sb.replace("要替换的字符串","替换后的字符串");
sb.clear()
sb.length = 0;
sb.Capactiy = 20;
System.Data.SqlClient
System.Data.OleDb
System.Data.Odbc
System.Data.OracleClient
System.Data.SqlClient
:SqlConnectionSystem.Data.OleDb
:OleDbConnectionSystem.Data.Odbc
:OdbcConnectionSystem.Data.OracleClient
:OracleConnectionSql Server身份验证:string 变量名 = "Data Source=服务器名; Initial Catalog=数据库名; User ID=用户名; pwd=密码"
服务器名:".
",表示本机
integrated security=true
表示采用信任模式连接(window身份验证登录)
Windows身份验证:Data Source=.;Initial Catalog=SecondHandCarDB;Integrated Security=True
不需要用户名和密码
SqlConnection 变量名 = new Sqlconnection(SQL连接字符串);
Open()
Close()
SqlCommand 变量名=new SqlCommand(执行语句, Connection对象);
ExecuteNonQuery()
ExecuteReader()
ExecuteScalar()
ValidateUser()
CheckLogin()
SqlDataReader SqlRead= SqlCom.ExecuteReader();
SqlRead["列名"]
StringBuilder sb = new StringBuilder();
while(sqlread.Read()){
sb.AppendFormat("[0]\t[1]",sqlread["id"],sqlread["content"]);
Console.WriteLine(sb);
sb.Length = 0;
}
FieldCound
Item
HasRows
Read()
Close()
GetName()
GetValue()
CommandBehavior.CloseConnection
SqlDataAdapter SqlAda = new SqlDataAdapter(统计语句, Con对象);
SqlAda.SelectCommand = Sqlcom;
new SqlCommandBuilder(DataAdapter对象);
Sqlada.操作命令 = new SqlCommandBuilder(Sqlada).Get对应操作命令
SqlAda.Fill(DataSet对象, DataSet里数据表的名称)
SqlAda.Update(DataSet对象, DataSet里数据表的名称)
DataSet ds = new DataSet(数据集的名称字符串(默认NewDataSet));
Tables
Count
ds.Tables["表名"].Rows.InsertAt(row, 下标)
Tables[].Copy()
DataRow row = DataSet对象.Tables["表名"].Rows[下标];
行:Rows[行的下标]
行里面的列:DataRow对象[列的下标]
新列:NewRow()
class 类名称
enum 枚举类的名称
class class1 {
}
enum class2 {
}
枚举类{ 内容1, 内容2 }
enum class1 {
"a", "b"
}
字符串1=整数1,字符串2=整数2
=
’ 左右的属性会互转Gender g = Gender.枚举属性
class 类名称
struct 结构类的名称
class class1 {
}
struct class2 {
}
struct s
struct class1{
public string name;
public int age;
public void aaa() {
Console.WriteLine("aaa");
}
}
// 第一种:报错
class Program
{
static void Main(string[] args)
{
Class1 c;
c.aaa();
Console.ReadKey();
}
}
// 第二种:正常
class Program
{
static void Main(string[] args)
{
Class1 c;
c.name = "张三";
c.age = 18;
c.aaa();
Console.ReadKey();
}
}
// 第三种:正常
class Program
{
static void Main(string[] args)
{
Class1 c = new c();
c.aaa();
Console.ReadKey();
}
}
using System.Xml
XmlDocument doc = new XmlDocument()
Load(xml路径)
DocumentElement
Name
InnerText
ChildNodes
XmlNodeList.Count
using System.IO
Exists
Extension
Copy(旧路径,新路径)
文件信息对象:FileInfo fi = new FileInfo(文件路径)
获取文件名:Name
获取文件后缀:Extension
获取文件的绝对路径:FullName
获取文件的字节数:Length
判断文件是否存在:Exists
复制文件:CopyTo(新路径)
剪切文件:MoveTo(新路径)
删除文件:Delete()
目录信息对象:DirectoryInfo di = new DirectoryInfo(根目录)
获取目录名:Name
获取当前目录的子目录对象数组:GetDirectories()
获取目录下所有文件对象,返回FileInfo[]:GetFiles()
FileStream fs = new FileStream(文件路径,读写方式)
Close()
写入器对象:StreamWriter sw = new StreamWriter(文件流对象)
写入文件:写入器.Write("写入内容")
关闭写入器:Close()
读取器对象:StreamReader sw = new StreamReader(文件流对象)
从文件的当前位置读取所有字符,返回string:读取器.ReadToEnd()
关闭读取器:Close()
等下还会出C#窗体笔记、C#面向对象笔记