C# 语言一.cs后缀结尾
.net平台只能跑.NET框架的程序
.NET的两种交互模式:
C/S 客户机/服务器
B/S 浏览器/服务器
屏幕打印输出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string name = "卡卡西";
int eag = 30;
string email = "kakaxi@qq.com";
decimal money = 5000m;
Console.WriteLine("我叫"+ name + "我住在火影村,我今年" + eag + "了我的邮箱是"+email + ",我的工资" + money);//加号起连接作用
Console.WriteLine("我叫{0}我住在火影村,我今年{1}了我的邮箱是{2},我的工资{3}", name, eag, email, money); //占位操作
Console.WriteLine("我叫{2}我住在火影村,我今年{1}了我的邮箱是{0},我的工资{3}", email, eag, name, money); //输出是按后面的顺序,依次输出
}
}
}
注释符号:
/// 多用于注释类或方法/函数
VS快捷键方式;
Ctrl+k+d 按d时k要释放 ====》自动对齐
Ctrl+j 弹出智能提示
home 跳到行首
end 跳到行尾
Ctrl+k+c k释放 快速注释
Ctrl+k+u k释放 撤销注释
F1 打开MSDN
折叠代码:
#region
#endregion
类型:
string 字符串类型
string s = " "; //字符串可以存储为 空, 字符类型不能存储为 空
decimal 金钱类型,用来存储金钱,值后面需要加上一个m
输入数据demo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("user you like eat fruit?");
string fruit = Console.ReadLine(); //输入数据只能以string类型接收
Console.WriteLine("haha, I like ear {0} too", fruit);
Console.WriteLine("Please input name six eag.");
string name = Console.ReadLine(); //输入数据
string six = Console.ReadLine();
string eag = Console.ReadLine();
Console.WriteLine("hello, {0} your six is {1} eag is {2}", name, six, eag);
}
}
}
命名规则:
- 骆驼命名:highSchoolStudent 多用于给变量命名
- Pascal 命名: HighShcool 多用于给类或方法命名
转义字符运用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello\nworld"); // \n终端输出换行 \r\n 写到文件中换行
Console.WriteLine("hello\tworld"); // \t tab键
Console.WriteLine(@"C:\Users\Administrator\OneDrive"); //取消转义原样输出
Console.WriteLine("C:\\Users\\Administrator\\OneDrive"); // \取消转义
char b = '\b';
Console.WriteLine("hello{0}world",b); //会删掉占位附的前一个字符,放在句头或句尾无用
Console.WriteLine("data:{0:0.00}", 10/3.0); //保留小数点后两位输出
}
}
}
运算符:
double 类型跟 int类型混合运算,会先把int类型转换为double类型运算。double优先级最高
int转double可以隐式转换。
double转int要强制类型转换。
Convert类型转换:
Convert使用内部调用的的int.parese / char.parese
int num = int.Parse(“dsfsa”); //调用方法
char a = char.Parse(“kfdjskfj”);
static void Main(string[] args)
{
string str = "123adc"; //里面有adc无法转换为double
string str1 = "123";
double d = Convert.ToDouble(str1); //任意类型都可以转,但要面上过的去才行
Console.WriteLine("str:{0}", str1);
Console.WriteLine("d:{0}", d);
}
一元运算符:
//一元运算符优先级最高,所以还是先++/--再乘除
int b = a++ + ++a * 2 + --a + a++; //5+7*2+6+6 = 31
Console.WriteLine(a); //a = 7
Console.WriteLine(b); //b = 31
bool类型:true/false
异常捕获:
try
{
//可能会执行出现异常的代码。
//只要这一行出现异常,这一行以下的代码都不执行
}
catch
{
//出现异常后该怎么做
}
变量的作用域:
从声明的大括号 { 开始到大括号 } 结束。
异常捕获实例:
static void Main(string[] args)
{
Console.WriteLine("please input year");
try
{
int year = Convert.ToInt32(Console.ReadLine()); //这句可能会输入异常
Console.WriteLine("please input month");
try
{
int month = Convert.ToInt32(Console.ReadLine()); //这句可能会输入异常
if (month <= 12 && month >= 1) //年份必须在12月内
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 9:
case 11:
day = 30;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 28;
}
else
{
day = 29;
}
break;
default:
day = 31;
break;
}
Console.WriteLine("year is {0} month {2} day {1}", year, day, month);
}
else
{
/* 月份大于没在1 - 12之间 */
Console.WriteLine("Input month error");
}
}
catch
{
/* 输入月份格式不对 */
Console.WriteLine("Input month mode error");
}
}
catch
{
/* 输入年份格式不对 */
Console.WriteLine("Input year demo error");
}
}
}
}
产生随机数:
static void Main(string[] args)
{
Random r = new Random(); //new 一个产生随机数的对象
int rNumber = r.Next(1, 10); //调用 Next这个方法,产生随机数的范围是 1 - 9 。
}
枚举语法:
[public] enum 枚举名
{
值1,
值2,
值3
…
}
枚举实例:
namespace ConsoleApplication1
{
/* 声明一个枚举类型 */
public enum sex
{
man,
woman
};
class Program
{
static void Main(string[] args)
{
sex she = sex.woman; //使用枚举
sex he = sex.man;
Console.WriteLine("she {0}, he {1}", (int)she, he); //枚举可以跟int之间相互转换
}
}
}
// 所有类型都可以转换为string类型
int num = 10;
string strNum = num.ToString();
Console.WriteLine(strNum); //输出10,字符类型的
//把字符串类型转为枚举
string student = "1"; //student = “woman” 也可转换,但是sex中没有该字符串的会报异常,如果是数字则不会
sex wangKing = (sex)Enum.Parse(typeof(sex), student); //调用方法实现
结构体:
[public] struct 结构体名
{
//包含的元素(字段)
string _name; //必须加_已示规范
}
结构体实例:
namespace ConsoleApplication1
{
public enum sex
{
man,
woman
}
public struct student
{
public string _name; //不加public下面点不出来,权限不够
public sex _sh;
public int _eag;
};
class Program
{
static void Main(string[] args)
{
sex she = sex.woman;
sex he = sex.man;
Console.WriteLine("she {0}, he {1}", (int)she, he);
student wangKing;
wangKing._eag = 30;
wangKing._name = "xiaoxue";
wangKing._sh = sex.woman;
}
}
}
数组:
数组类型[] 数组名 = new 数组类型[数组长度]
//数组的几种声明方式
int[] num1 = new int[3]{1,2,3};
int[] num2 = new int[]{1,2,3,4,5};
int[] num3 = {1,2,3,4};
int[] num = new int[10];
num[2] = 3;
数组实例:
static void Main(string[] args)
{
string str = null;
string[] name = { "laoyang", "laosu", "laoliu", "laoma", "laowang", "laozhou", "laolu"};
for (int i = 0; i < name.Length; i++)
{
str += name[i] + "|"; //字符串拼接
}
Console.WriteLine(str);
}