Unity—入门C#学习之初识C#

/*using System;//引用命名空间,using是引用的意思,System是命名空间
//namespace创建命名空间,命名空间是一个比文件域小一点的作用域,
//不同的命名空间中不用担心重名
namespace _1 
{
//class是声明类的关键字,C#是完全面向对象的语言,所有的函数和变量都是放在
//类中定义的
//Main函数是程序的入口,一个程序只能有一个Main函数,包含Main函数的类叫做主类
//所以一个程序只能有一个主类
class MainClass 
{
public static void Main()
{
Console.WriteLine("Hello World!");//在屏幕上输出一句话
//所有的函数都是放在类中定义的,所以访问函数时要通过类(或对象)去访问
}
}
}
namespace aa
{


}
*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
Console.WriteLine("hello world");
}
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
Console.WriteLine("helloworld");
Console.WriteLine("abc");
Console.Write("bcd");
Console.Write("efg");
int a = 5;
int b = 4;
int c = 3;
int d = 2;
int e = 1;


Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine("a+b={0}",a+b);//a+b=15
Console.WriteLine("a={0} b={1}",a,b);


Console.WriteLine("{4},gfdgfdg{4},{4},{4},{4}",b,c,d,e,a);


}
}
}*/
/*using System;
namespace aaa
{
class MainClass
{
public static void Main()
{
short a = 5; //2
long b = 10;    //8


int c = a;
//int d = b;
//C#中隐式类型转换的规则:只能从低精度自动转成高精度,
//由高精度到低精度只能强制类型转换


float e = 5.5f;
double f = 5.5;
//C#中所有的浮点数默认是double类型,浮点数后加f表示是float型
//加d表示double,什么也不加默认是double


//强制类型转换
//1.括号加类型
e = (float)f;


//2.使用Convert类中的方法(函数)
//c = b;
c = Convert.ToInt32(b);
//short g = b;
short g = Convert.ToInt16(b);


e = Convert.ToSingle(f);
f = Convert.ToDouble(e);


//3.使用Parse方法,主要用来将字符串转成数字
string s = "helloworld";
string s2 = "12345";
int s22 = int.Parse(s2);
Console.WriteLine(s22);
string s3 = "5.5";
double s33 = double.Parse(s3);
float s333 = float.Parse(s3);
//int s3333 = int.Parse(s3);
float s222 = float.Parse(s2);
}
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
string a = "helloworld";//字符串是引用类型
a = "abcdef";//a中保存的是字符串的地址,修改的是a的指向
Console.WriteLine(a);//栈上的指针指向托管堆中的字符串


Console.WriteLine(a[0]);
Console.WriteLine(a[1]);
Console.WriteLine(a[2]);
Console.WriteLine(a[3]);
Console.WriteLine(a[4]);
}
}
}*/
/*using System;
namespace aaa
{
class MainClass
{
public static void Main()
{
color a = color.green;
//创建枚举变量不用加enum,访问枚举值用枚举类型.枚举值
Console.WriteLine(a);
int aa = Convert.ToInt32(a);
Console.WriteLine(aa);
}
}


enum color { red=3, green, blue, yellow }
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
//struct Student s;
Student s1;//2.定义结构体变量不用加struct
  //Student s2 = { "zhangsan", 20, '男' };
  //3.C#中结构体的初始化要用构造函数初始化
s1.name = "zhangsan";
s1.age = 20;
s1.sex = 'f';
//4.C#中结构体的成员默认都是私有的,不能在结构体外访问
}
}


struct Student
{
string name;
int age;
char sex;
}//1.不用分号
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
Student s;
s.name = "zhangsan";
s.age = 20;
s.sex = '男';


Console.WriteLine("{0},{1},{2}", s.name, s.age, s.sex);


//Student student = {"zhangsan",20,'f' };
//C#结构体初始化要使用构造函数初始化
}
}


struct Student
{
public string name;//public表示公有成员,公有成员可以在结构体外被访问
public int age;
public char sex;
//private int a;//private表示私有成员,私有成员只能在当前结构体内访问,出了结构体就不能访问了。
//如果不加public或private默认是私有
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
Student s = new Student("张三", 20, '男');
//构造函数:在结构体中定义的一个函数,用来初始化结构体中每个成员的
//通过new关键字加构造函数来初始化结构体变量
Console.WriteLine("name:{0},age:{1},sex:{2}", s.name, s.age, s.sex);
Student s2 = new Student("李四", 22, '男');
Console.WriteLine("name:{0},age:{1},sex:{2}", s2.name, s2.age, s2.sex);
}
}
struct Student
{
public string name;
public int age;
public char sex;


//构造函数的特点:
//1.一般是公有的
//2.构造函数的名字和结构体的名字一模一样
//3.构造函数没有返回值,连void也不写
//4.在构造函数中要对每一个成员都初始化


public Student(string _name, int _age, char _sex)
{
name = _name;
age = _age;
sex = _sex;
}
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
//int a[5] = { 1, 2, 3, 4, 5 };
int[] a = { 1, 2, 3, 4, 5 };
Console.WriteLine(a[0]);
Console.WriteLine(a[1]);
Console.WriteLine(a[2]);
Console.WriteLine(a[3]);
Console.WriteLine(a[4]);




int[] b = new int[5];
b[0] = 1;
b[1] = 1;
b[2] = 1;
b[3] = 1;
b[4] = 1;


for (int i = 0; i < 5; i++)
{
Console.WriteLine(a[i]);
}
}
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5 };
int[] b = a;//数组是引用类型,b和a都是保存的数组的首元素地址
b[0] = 100;
b = new int[5] { 5, 4, 3, 2, 1 };//在C#中数组名的值可以改,即指向新的数组
for (int i = 0; i < 5; i++)
{
Console.WriteLine(a[i]);
}
}
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++)
{
Console.WriteLine(a[i]);
}
//访问数组中的每个元素称为数组遍历
//快速遍历
//每次循环创建一个临时变量temp,接收数组中遍历到的元素
foreach (int temp in a)
{
Console.WriteLine(temp);
}
}
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
string[] a = { "aaa", "bbb", "ccc" };
foreach (string aaa in a)
{
Console.WriteLine(aaa);
}
}
}
}*/
/*using System;
namespace aa
{
class MainClass
{
public static void Main()
{
int a, b, c;
Console.WriteLine("输入三个整数");
string t = Console.ReadLine();//读入一整行内容,返回字符串类型
a = int.Parse(t);


b = int.Parse(Console.ReadLine());
c = int.Parse(Console.ReadLine());


Console.WriteLine("{0} {1} {2}", a, b, c);


int d = Console.Read();//读入一个字符,返回该字符的asc码
Console.WriteLine(d);
char dd = Convert.ToChar(d);
Console.WriteLine(dd);






}
}
}*/
using System;
namespace aa
{
class MainClass
{
public static void Main()
{
int[] a = new int[5];
for (int i = 0; i < 5; i++)
{
a[i] = int.Parse(Console.ReadLine());
}


foreach (int temp in a)
{
Console.WriteLine(temp);
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值