C#的基本操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
delegate double mydelegate(double x, double y); //声明一个代理类型
class myDeclass //声明一个类
{
public double add(double x, double y)
{
return x + y;
}
public double sub(double x, double y)
{
return x - y;
}
}
class Program //程序类
{
static void Main(string[] args) //包含要完成本控制台功能的主程序
{ //Console.ReadLine() 获取控制台输入的字符串
//若要输入数值,需要数据类型的转换 eg:arr = int.Parse(Console.ReadLine())
//Console.read() (返回的ASCELL值,类似C中的char) 读取键盘输入的第一个字符
//Console.readkey() 按任意键退出
//string name = "Hang meimei";
//Console.WriteLine(name);
Console.WriteLine("Hello World ?");
//Console.WriteLine("Hello World" + name);
//Console.WriteLine("我的姓名是: {0}",name);
//{N,M:格式化字符串} {}表示字符串插入的变量值;N输出变量的序号;M输出的变量所占字符的个数;指定输出的字符串的格式
//eg:Console.WriteLine("{0:d4}",123) 输出:0123
//Console.Write() WriteLine会换行,Write不会换行
//基本规则
//标识符 字母、数字、下划线;第一个字母或者下划线;大小写敏感;不能重复
//数据类型
//结构体
//struct 结构名(
// public 数据类型 域名;
//)
//结构体不能继承
//结构体是"value types" 存储在栈中
//类是引用类型,存储在栈(存储地址)和堆中
//枚举类型
//enum fruit{Apple, Banana, Orange};
//引用类型
//Object
//类
//指代
myDeclass obj = new myDeclass(); //声明一个类的对象
mydelegate p = new mydelegate(obj.add); //申明一个指代类型的变量 讲函数赋入
Console.WriteLine("{0}", p(5, 8));
//接口
//数组
string[] myarray ={"ab","aa","c","ddd"}; //定义一个一维数组
//string[,] twarray; 定义一个二维数组
//动态初始化
int[] a = new int[10] { 1,2,3,4,5,6,7,8,9,10};
//int[] a = new int[]{1,2,3,4,5,6,7,8,9,10};
//int[] a = new int[2] {1} 不允许
//不允许数组的长度为变量
//静态初始化(变量和内容不能分开声明)
int[] myarr = { 1, 2, 3, 4 };
//字符串类型
//"A" 字符串类型 'A' char类型
//隐式转换、强制转换
//强制转换
int brr = 5;
Convert.ToInt32(brr);
//Convert.ToBoolean(brr); brr必须为ture或flase
//Convert.ToChar(brr);
//Convert.ToDateTime(brr);转换为日期型
//Convert.ToDecimal(brr);转换成定点型
//foreach循环
int[] intArray ={ 2,3,4,1};
foreach (int i in intArray)
{
Console.WriteLine("i =" +i);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{ public class Car //与C++不同,只能在类中定义函数
//protect、private、public
//Internal 内部成员型,只有本程序的成员可以用
//abstract 抽象类,不允许声明对象
{
//#####################################
//属性
int x,y;
public int px
{ //set和get可以有一个,也可以都有,但不可以都没有
//只有set时,只能读;只有get时,只能写
get //get访问器
{
return x;
}
set //set访问器
{ if(value <10)
x = value;
//限制对x值得操作
}
}
public int py
{
get
{
return y;
}
set
{
y = value;
}
}
//######################
public string model;
public string color;
public int yearBuilt;
//定义域
public Car() { } //默认的构造函数
public Car(int x1, int y1) //带参数的构造函数
{
x = x1;
y = y1;
}
~Car() { } //析构函数
//先析构后创建的对象
public void Start()
{
System.Console.WriteLine(model + "started.");
}
public void Stop()
{
System.Console.WriteLine(model + "stopped.");
}
//同名函数(方法)允许存在,区别在于参数值的个数和类型
};
class Program
{
static void Main(string[] args)
{
Car mycar;
mycar = new Car();
//如果在类中没有定义,默认创建了一个构造函数
mycar.model = "Toyota";
mycar.color = "red";
mycar.yearBuilt = 2010;
Car p = new Car();
p.px = 3;
p.py = 8;
//属性写操作
Console.WriteLine("点=>({0},{1})", p.px, p.py); //属性读操作
//属性的加入是为了限制变量的操作
}
}
}