您的位置:首页 > 编程语言 > C#

C#初级篇---类型转换、枚举、结构体、数组、函数、

2015-10-01 16:31 766 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _004_练习_定义变量存储主角的信息 {
class Program {
static void Main(string[] args)
{
string name = "siki";
int hp = 100;
int level = 34;
float exp = 345.3f;
long l = 10000000000000L;
Console.WriteLine("主角的名字是:\"{0}\"   \n血量:{1}   \n等级:{2}   \n经验值:{3}", name, hp, level, exp);
Console.ReadKey();
}
}
}




console基本使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _005_在字符串前面加上_字符 {
class Program {
static void Main(string[] args)
{
//当在字符串前面加上一个@字符的时候,我们就可以把一个字符串定义在多行
// 编译器不会再去识别字符串中的转义字符
// 如果需要在字符串中表示一个双引号的话,需要使用两个双引号
string str1 = @"I'm a good man.
You are bad girl!";
Console.WriteLine(str1);
string str2 = @"I'm a good man. You are bad girl!";
Console.WriteLine(str2);

//使用@字符的第二个好处
string path = "c:\\xxx\\xx\\xxx.doc";
Console.WriteLine(path);
string path2 = @"c:\xxx\xx\xxx.doc";
Console.WriteLine(path2);
Console.ReadKey();
}
}
}




读取键盘输入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _009_从键盘上读取输入的字符串 {
class Program {
static void Main(string[] args)
{
string str = Console.ReadLine();//用来接收用户输入的一行文本,也叫做一行字符串,按下换行就是回车的时候结束
Console.WriteLine(str);

string str1 = "123";
int num1 = Convert.ToInt32(str1);//这个方法可以把一个整数的字符串转化成整数
Console.WriteLine(num1);

string str2 = Console.ReadLine();
int num2 = Convert.ToInt32(str2);
Console.WriteLine(num2);

string str3 = Console.ReadLine();
double num3 = Convert.ToDouble(str3);//这个可以把用户输入的小数的字符串转化成double浮点类型
Console.WriteLine(num3);

Console.ReadKey();
}
}
}




类型转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _010_接收输入_显示和 {
class Program {
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字");
string str1 = Console.ReadLine();
int num1 = Convert.ToInt32(str1);
Console.WriteLine("请输入第二个数字");
string str2 = Console.ReadLine();
int num2 = Convert.ToInt32(str2);
int sum = num1 + num2;
Console.WriteLine("您输入的两个数字的和为");
Console.WriteLine(sum);
Console.ReadKey();
}
}
}




类型转换

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _036_显示转换和隐式转换 {
class Program {
static void Main(string[] args)
{
string str = "123.3";
double num = Convert.ToDouble(str);//当字符串里面存储的是整数的时候,就可以转化成int double类型,否则出现异常

//当字符串里面是一个小数的时候,就可以转化成double类型
int mynum = 234234;
string str2 = Convert.ToString(mynum);//它可以把一个int float double byte类型转换成字符串
string str3 = mynum + "";//一个int float double类型直接加上一个空的字符串,相当于把这个数字转化成一个字符串
Console.WriteLine(num);
Console.WriteLine(str2);
Console.WriteLine(str3);

Console.ReadKey();
}
}
}




枚举

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _037_枚举类型 {
//枚举类型的定义
enum GameState:byte//修改该枚举类型的存储类型,默认为int
{
Pause = 100, // 默认代表的是整数0
Failed = 101,// 默认代表的是整数1
Success=102,// 默认代表的是整数2
Start=200// 默认代表的是整数3
}
class Program
{
static void Main(string[] args) {
////利用定义好的枚举类型 去声明变量
GameState state = GameState.Start;
if (state == GameState.Start)//枚举类型比较
{
Console.WriteLine("当前处于游戏开始状态");
}
Console.WriteLine(state);

//枚举类型转为int类型,需要强制置换符
GameState state1 = GameState.Start;
int num = (int)state1;
Console.WriteLine(num);
Console.ReadKey();
}
}
}




结构体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _038_结构体 {
//我们可以把结构体当成,几个类型组成了一个新的类型
//比如下面的这个就是使用了3个float类型的变量,来表示一个坐标类型
struct Position
{
public float x;
public float y;
public float z;
}

enum Direction
{
West,
North,
East,
South
}

struct Path
{
public float distance;
public Direction dir;
}
class Program {
static void Main(string[] args)
{
//当使用结构体声明变量的时候,相当于使用结构体中所有的变量去声明
Position enemy1Position;
//可以通过.加上属性名来访问结构体中指定的变量
enemy1Position.x = 34;

Path path1;
path1.dir = Direction.East;
path1.distance = 1000;
}
}
}

数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _040_数组的遍历 {
class Program {
static void Main(string[] args)
{
int[] scores = {23, 2, 32, 3, 34, 35, 45, 43, 543};

foreach (int temp in scores)//foreach会依次取到数组中的值,然后赋值给temp,然后执行循环体 (数据类型 标识符 in 表达式)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
}




参数数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _050_参数数组__定义一个参数个数不确定的函数_ {
class Program {
static int Sum(int[] array)//如果一个函数定义了参数,那么在调用这个函数的时候,一定要传递对应类型的参数,否则无法调用(编译器编译不通过)
{
int sum = 0;
for (int i = 0; i < array.Length; i++)
{
sum += array[i];
}
return sum;
}

static int Plus(params int[] array)//这里定义了一个int类型的参数数组,参数数组和数组参数(上面的)的不同
//在于函数的调用,调用参数数组的函数的时候,我们可以传递过来任意多个参
//<strong><span style="color:#3333FF;">然后编译器会帮我们自动组拼成一个数组,参数如果是上面的数组参数,那么这个数组我们自己去手动创建</span></strong>
{
int sum = 0;
for (int i = 0; i < array.Length; i++) {
sum +=
b7a4
array[i];
}
return sum;
}
static void Main(string[] args)
{
int sum = Sum(new int[] {23, 4, 34, 32, 32, 42, 4});
Console.WriteLine(sum);
int sum2 = Plus(23, 4, 5, 5, 5, 32, 423, 42, 43,23,42,3);//参数数组就是帮我们 减少了一个创建数组的过程
Console.WriteLine(sum2);
Console.ReadKey();
}
}
}




函数定义

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _048_函数的定义 {
class Program {
static void Write()// void 表示空的返回值,就是不需要返回值
{
//这里是函数体也叫做方法体,这里可以写0行 ,一行或者多行语句
Console.WriteLine("Text output from function");
return;//这个语句用来结束当前函数
}

static int Plus(int num1,int num2)//函数定义的时候,参数我们叫做形式参数(形参),num1跟num2在这里就是形参,形参的值是不确定的
{
int sum = num1 + num2;
return sum;
}
static void Main(string[] args) {
Write();//函数的调用   函数名加上括号 ,括号内写参数
int num1 = 45;
int num2 = 90;
int res1 = Plus(num1, num2);//当调用函数的时候,这里传递的参数就是实际参数(实参),实参的值会传递给形参做运算
int res2 = Plus(45, 20);//这里定义了res1和res2来接受方法的返回值
Console.WriteLine(res1);
Console.WriteLine(res2);
Console.ReadKey();

}
}
}




结构体函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;

namespace _051_结构函数的定义和使用 {
struct CustomerName
{
public string firstName;
public string lastName;

public string GetName()
{
return firstName + " " + lastName;
}
}

struct  Vector3
{
public float x;
public float y;
public float z;

public double Distance()
{
return Math.Sqrt(x*x + y*y + z*z);
}
}
class Program {
static void Main(string[] args)
{
CustomerName myName;
myName.firstName = "siki";
myName.lastName = "Liang";
Console.WriteLine("My name is "+myName.GetName());

Vector3 myVector3;
myVector3.x = 3;
myVector3.y = 3;
myVector3.z = 3;
Console.WriteLine(myVector3.Distance());
Console.ReadKey();
}
}
}



函数重载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _052_函数的重载 {

class Program {
static int MaxValue(params int[] array)
{
Console.WriteLine("int类型的maxvalue被调用 ");
int maxValue = array[0];
for (int i = 1; i < array.Length; i++)
{
if (array[i] > maxValue)
{
maxValue = array[i];
}
}
return maxValue;
}

static double MaxValue(params double[] array)
{
Console.WriteLine("double类型的maxvalue被调用 ");
double maxValue = array[0];
for (int i = 1; i < array.Length; i++) {
if (array[i] > maxValue) {
maxValue = array[i];
}
}
return maxValue;
}
static void Main(string[] args)
{
int res = MaxValue(234, 23, 4);//编译器会根据你传递过来的实参的类型去判定调用哪一个函数
double res2 = MaxValue(23.34, 234.5, 234.4);
Console.WriteLine(res);
Console.WriteLine(res2);
Console.ReadKey();
}
}
}




函数委托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _053_委托的使用 {
//定义一个委托跟函数差不多,区别在于
//1,定义委托需要加上delegate关键字
//2,委托的定义不需要函数体
delegate double MyDelegate(double param1, double param2);
class Program {
static double Multiply(double param1, double param2)
{
return param1*param2;
}

static double Divide(double param1, double param2)
{
return param1/param2;
}
static void Main(string[] args)
{
MyDelegate de;//1、利用我们定义的委托类型声明了一个新的变量
de = Multiply;//2、当我们给一个委托的变量赋值的时候,返回值跟参数列表必须一样,否则无法赋值

Console.WriteLine(de(2.0, 34.1));
de = Divide;
Console.WriteLine( de(2.0,34.1) );
Console.ReadKey();
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: