命名规范
Camel命名: 首字母小写,其余首字母大写,多用于变量和字段
Pascal命名:每个单词首字母都大写,用于类和方法
运算符
关系运算符:>, <,==,>=,!=
逻辑运算符:且&&, 或||, 非!
解决方案(公司)>项目(部门)>类(员工)
.sln
是解决方案文件,包含了整个文件的信息,可以书双击启动
using System; //引用命名空间
namespace test2 //项目命名空间,test2为项目名称
{
class Program //类,Program 类名
{
static void Main(string[] args) //程序主入口 main 函数,必须写在这里面
{
Console.WriteLine("Hello World!");
}
}
}
数据类型
数字
namespace _01class
{
class fxx
{
static void Main(string[] args)
{
//声明变量
int student_number = 1001; //整型
float fruit_price = 6.6f; //单精度
double fruit_price_small = 3.145666; //双精度,一般用double
Console.WriteLine(student_number);
Console.WriteLine(fruit_price);
Console.WriteLine(fruit_price_small);
Console.ReadKey();
}
}
}
字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _01class
{
class fxx
{
static void Main(string[] args)
{
char xx_name = 'M'; //一个字符,一个字母,一个符号
string xx_text = "SDFSDFSDFDS123123"; // 多个
bool is_true = true; //布尔类型
const string setting = "123124sdfdsfdsdsfdsfsddsfsdf";
long gg_text =5523423;
}
}
}
运算
namespace _01class
{
class fxx
{
static void Main(string[] args)
{
//前++后++;单独使用没区别;但是使用表达式的时候有区别,
int a = 10;
int b = a++; //先把a的值赋值给b,a在进行+1的操作;
int c = ++a; //a先+1操作,之后,再把结果赋值给c;
//Console.WriteLine(number);
Console.WriteLine(b);
Console.WriteLine(c);
Console.ReadKey();
}
}
}
>>>
10
12
类型转换
namespace _01class
{
class fxx
{
static void Main(string[] args)
{
//类型转换;
//隐式转换:不需要声明就能进行的转换
int a = 10;
float b;
b = a;
Console.WriteLine("b的值是{0}", b);
//显式转换:也可以叫强制转换,强制转为我们要的
double aa = 3.414214124;
int bb = (int)aa;
Console.WriteLine(bb);
//convert转换:不是同一个大类的转换,失败报错
String aaa = "1231324123123123" ;
double bbb = Convert.ToDouble(aaa);
Console.WriteLine(bbb);
}
}
}
>>>
b的值是10
3
1231324123123123
- 方法二
namespace cSharpTest
{
class fxx
{
static void Main(string[] args)
{
//same as convert
int number01 = int.Parse("123123");
//same as try catch
int number02 = 0;
//TryParse is a function; bool is a return value;
bool numFlag = int.TryParse("123123df", out number02);
Console.WriteLine(numFlag);
Console.WriteLine(number02);
}
}
}
if 判断
namespace _01class
{
class fxx
{
static void Main(string[] args)
{
int a = 10;
if (a>10)
{
Console.WriteLine("a大于10");
}
else
{
Console.WriteLine("a不大于10");
}
}
}
}
if ---else if --else
namespace _01class
{
class fxx
{
static void Main(string[] args)
{
int a = 0;
if (a>10)
{
Console.WriteLine("a大于10");
}
else if (a==10)
{
Console.WriteLine("a等于10");
}
else if (a==0)
{
Console.WriteLine("a是空的");
}
else
{
Console.WriteLine("a小于10");
}
}
}
}
占位符
namespace _02class
{
class fxx
{
static void Main(string[] args)
{
string name = "fxx";
string sex = "female";
int age = 19;
Console.WriteLine("My name is "+name);
//占位符{0},{1}
Console.WriteLine("我叫{0},\n我的性别是{1},\n我的年龄是{2}",name, sex, age);
}
}
}
综合:简易计算器
namespace _04class
{
class fxx_caculation
{
static void Main(string[] args)
{
Console.WriteLine("Please inpute a number :");
double _01numer = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please inpute another number:");
double _02number = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please inpute operational character:");
string _new_character = Console.ReadLine();
double _result = 0;
if (_new_character == "+")
{
_result = _01numer + _02number;
}
else if(_new_character == "-")
{
_result = _01numer - _02number;
Console.WriteLine(_result);
}
else
{
Console.WriteLine("Please inpute a correct character");
}
Console.WriteLine("Result is {0}",(float)_result);
}
}
}
switch
namespace _03class
{
class _03fxx
{
enum Mydate
{
Mon=1,
Tue=2,
Wed=3,
}
static void Main(string[] args)
{
Mydate myDate = Mydate.Mon;
int _one_date = (int)myDate;
switch (_one_date)
{
case 1:
Console.WriteLine("Today is Monday");
break;
case 2:
Console.WriteLine("Today is Tuesday");
break;
default:
break;
}
}
}
}
for循环
//1+..+100
namespace _03class
{
class _03fxx
{
static void Main(string[] args)
{
//for(初始表达式,条件表达式,增量表达式),按两下Tab
int _result = 0;
for (int i = 1; i <= 100; i++) {
/*Console.WriteLine(i);*/
//
_result += i;
}
Console.WriteLine("Sum is {0}", _result);
}
}
}
while 循环
//1+..+100
namespace _03class
{
class _03fxx
{
static void Main(string[] args)
{
int i = 1;
int sum_i = 1;
while (i<100)
{
i++;
sum_i += i;
}
Console.WriteLine("Sum is {0}", sum_i);
Console.ReadKey();
}
}
}
数组
namespace cSharpTest
{
class fxx
{
static void Main(string[] args)
{
//默认0
int[] nums = new int[10];//定长数组,超出报索引错
//默认null
string[] strs = new string[10];
//默认fasle
bool[] bools = new bool[10];
//声明时,直接赋值
int[] new_nums = { 1, 2, 3, 4};
//未知长度空数组
int[] unknow_nums = { };
}
}
}
namespace cSharpTest
{
class fxx
{
static void Main(string[] args)
{
//将数组{jay tom mac}放去字符串jay|tom|mac
string[] names = { "jay", "tom", "jack " };
string strs= "";
for (int i = 0; i < names.Length-1; i++)
{
strs += names[i] + "|";
}
Console.WriteLine(strs+names[names.Length-1]);//解决最后一个jack|
}
}
}
函数传参
namespace _04class
{
class _04fxx
{
//函数传参
static void Main(string[] args)
{
string name = "Fxx";
int age = 18;
Hello(name,age);
}
static void Hello(string name,int age)
{
Console.WriteLine("Hello,{0},your age is {1}",name,age);
}
}
}
return
namespace _04class
{
class _04fxx
{
//函数传参
static void Main(string[] args)
{
int _01sum = _04fxx_add(5);
Console.WriteLine(_01sum);
}
static int _04fxx_add(int number)
{
int reInt = 10 + number; //如果不想声明函数,最好直接使用
return reInt;
}
}
}
函数重载
namespace _04class
{
class _04fxx
{
static void Main(string[] args)
{
ReRusult("123", "41424");
ReRusult(10, 100);
}
static void ReRusult(int _num01, int _num02)
{
int _num03 = _num01 + _num02;
Console.WriteLine(_num03);
}
//函数重载
static void ReRusult(string _str01,string _str02)
{
string _str3 = _str01 + _str02;
Console.WriteLine(_str3);
}
}
}
高级参数ref,out
namespace _04class
{
class _04fxx
{
static void Main(string[] args)
{
int _num01 = 20;
Addnumber(ref _num01);
Console.WriteLine(_num01);
}
static void Addnumber( ref int _num01)
{
_num01 += 10;
}
}
}
异常捕获
namespace cSharpTest
{
class fxx
{
static void Main(string[] args)
{
int number = 0;
Console.WriteLine("Please inpute a number");
try
{
number = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(number * 100);
}
catch (Exception)
{
Console.WriteLine("You can not transform this number");
}
}
}
}
改良版
namespace cSharpTest
{
class fxx
{
static void Main(string[] args)
{
int number = 0;
bool num_flag = true;
Console.WriteLine("Please inpute a number");
try
{
number = Convert.ToInt32(Console.ReadLine());
}
catch (Exception)
{
num_flag = false;
Console.WriteLine("You can not transform this number");
}
if (num_flag==true)
//if(b)
{
Console.WriteLine(number * 100);
}
}
}
}
三元表达式
namespace cSharpTest
{
class fxx
{
static void Main(string[] args)
{
int n1 = 10;
int n2 = 33;
//normal
if (n1 > n2)
{
Console.WriteLine(n1);
}
else
{
Console.WriteLine(n2);
}
//ternary expression
int max = n1 > n2? n1: n2;
//ternary expression
string name = "jay";
string is_human= name == "jack" ? "pig" : "humanbeing";
Console.WriteLine(is_human);
}
}
}
常量
namespace cSharpTest
{
class fxx
{
static void Main(string[] args)
{
const int number1 = 1;//常量,
int number1 = 2;//报错,不能重新被赋值
}
}
}
枚举
枚举同样是一个变量类型
public:访问修饰符,公开,哪里都可以访问。
eum:枚举名,规范开发,类似于性别,是否,这样的选项可以用枚举,不可以在main里面枚举,一般写在命名空间的下面,类的外面,表示所有的类都可以用这个枚举。
namespace cSharpTest
{
public enum Gender{
male,
femal,
}
class fxx
{
static void Main(string[] args)
{
Gender gender = Gender.male;//必须声明变量后,才能使用
Console.WriteLine(gender);
}
}
}
- 枚举转为int,string
namespace cSharpTest
{
public enum Gender{
male,
femal,
}
class fxx
{
static void Main(string[] args)
{
//默认与int互相转,根据index赋值
int n =(int)Gender.male;//强制转换
Console.WriteLine(n);//0
//所有类型都用ToString()可以转为string
string n2 = Convert.ToString(Gender.male);
string n3 = Gender.femal.ToString();//常用这中方式转换字符串
Console.WriteLine(n2);
Console.WriteLine(n3);
}
}
}
- string 转为 enum
namespace cSharpTest
{
public enum QQsate
{
}
class fxx
{
static void Main(string[] args)
{
string s = "0";
QQsate state = (QQsate)Enum.Parse(typeof(QQsate), s);
}
}
}
结构
namespace cSharpTest
{
//帮助我们一次性声明多个不同类型的变量;
//变量在程序运行期间只能储存一个值;
//为了和变量区分加加下滑线
public struct Person
{
public string _name;
public int _age;
public Gender _gender;
}
public enum Gender
{
male,
female,
}
class fxx
{
static void Main(string[] args)
{
Person zsPeron;
zsPeron._name = "zhang";
zsPeron._age = 18;
zsPeron._gender = Gender.female;
Person jayPeron;
jayPeron._name = "jay";
jayPeron._age = 40;
jayPeron._gender = Gender.male;
Console.WriteLine(zsPeron._gender);
Console.WriteLine(jayPeron._gender);
}
}
}
网友评论