/*
* 2018年6月20日20:01:33
* 代码目的:
* 演示命名空间由类组成。类由方法和属性构成。
* 属性的权限有public, protected, private组成
* 并且程序的入口从某个类的static void Main()函数开始
* 在类中的普通成员可以在方法中自由访问。
* 演示访问权限的使用
* 输出到控制台Console.WriteLine函数的演示,并且打印变量值的代码控制。
* 演示C#中类型转换
* 演示C#中string类型的使用
*/
using System;
namespace RectangleApplication
{
class Rectangle
{
// 成员变量
public double length;
double width;
public void Acceptdetails()
{
length = 4.5;
width = 3.5;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
//打印某个变量,用{0}, {1}表示
Console.WriteLine("Length: {0}, Width: {1}", length, width);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
//受保护级别限制
//r.length = 5;
r.Acceptdetails();
r.Display();
r.length = 4;
r.Display();
//转义字符'\'
string str = "C:\\Windows";
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.WriteLine("Size of uint: {0}", sizeof(uint));
Console.WriteLine("{0}", str);
Console.WriteLine("{0}", str.Length);
Console.WriteLine("=======================");
string[] a1 = new string[] { "a", "b", "c" };
string[] a2 = a1;
for (int i = 0; i < a2.Length; i++)
{
Console.Write(a2[i] + " "); //a b c
}
a1[2] = "d";
Console.WriteLine(); //换行
for (int i = 0; i < a2.Length; i++)
{
Console.Write(a2[i] + " "); //a b d
}
Console.WriteLine();
Console.WriteLine("========================");
Console.WriteLine("String 是一种特殊的引用类型");
string a = "123";
string b = a;
Console.WriteLine(a + " " + b); //123 123
//此处需要注意
b = "456";
Console.WriteLine(a + " " + b); //123 456
Console.WriteLine("===============");
Console.WriteLine("类型转换");
double d = 5673.74;
int ia;
// 强制转换 double 为 int
ia = (int)d;
Console.WriteLine(ia);
ia = 75;
float f = 53.005f;
bool b1 = true;
Object oo = f;
Console.WriteLine(ia.ToString());
Console.WriteLine(f.ToString());
Console.WriteLine(d.ToString());
Console.WriteLine(b1.ToString());
Console.WriteLine();
Console.WriteLine("=============");
Console.WriteLine("类型之间的转换");
string locstr = 123.ToString();
//如果要将"locstr"转成整型数
int ii;
//方法一: 用 Convert
ii = Convert.ToInt16(locstr);
Console.WriteLine("ii: {0}", ii);
//方法二: 用 Parse
ii = int.Parse(locstr);
Console.WriteLine();
Console.WriteLine("=============");
string s1 = "abcd";
string s2 = "1234";
int aa, bb;
bool bo1 = int.TryParse(s1, out aa);
Console.WriteLine(s1 + " " + bo1 + " " + aa);
bool bo2 = int.TryParse(s2, out bb);
Console.WriteLine(s2 + " " + bo2 + " " + bb);
try
{
Console.WriteLine("输入数字,将计算出它加一的答案");
int aaa = int.Parse(Console.ReadLine()); //有可能会抛出异常
Console.WriteLine("答案是{0}", ++aaa); //如果没有异常,程序才会进入这一步
}
catch (Exception)
{
Console.WriteLine("无法转换"); //如果捕获到异常,就说“无法转换”
}
Console.ReadLine();
}
}
}
/*
Length: 4.5, Width: 3.5
Width: 3.5
Area: 15.75
Length: 4, Width: 3.5
Width: 3.5
Area: 14
Size of int: 4
Size of uint: 4
C:\Windows
10
=======================
a b c
a b d
========================
String 是一种特殊的引用类型
123 123
123 456
===============
类型转换
5673
75
53.005
5673.74
True
=============
类型之间的转换
ii: 123
=============
abcd False 0
1234 True 1234
输入数字,将计算出它加一的答案
3
答案是4
*/
C#基础知识----基本类型和类型转换
猜你喜欢
转载自blog.csdn.net/lk142500/article/details/80757803
今日推荐
周排行