using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace New2
{
class Program
{
static void Main(string[] args)
{
/*数组示例1==================================================
int[] a = new int[5];//初始化数组a,共有5个元素,从a[0]到a[4]
for (int i = 0; i < a.Length; i++)
{
a[i] = i * i;
}
Console.WriteLine("a.Length={0}",a.Length );
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("a[{0}]={1}",i,a[i]);//{0}代表第一个参数位置,{1}代表第二个参数位置
}
*/
//===========================================================
/*数组示例2===========================================================
int[] a = new int[6]{3,4,2,7,5,0};//在定义数组的同时初始化数组,共有6个元素,从a[0]到a[5]
Console.WriteLine("a.Length={0}", a.Length);
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("a[{0}]={1}", i, a[i]);//{0}代表第一个参数位置,{1}代表第二个参数位置
}
//Console.WriteLine("a[5]={0},a[6]={1}", a[5], a[6]);//这种情况下数组越界,程序抛出异常。说明C#的安全好
Console.WriteLine("a[5]={0}", a[5]);
* /
//==============================================================
/*比较示例=============================================================
string s1 = "china";
//string s2 = s1;//这种情况下object)s2==(object)s1返回True
string s2 = string.Copy(s1);//这种情况下object)s2==(object)s1返回False
//string s2 = "china";//这种情况下object)s2==(object)s1返回True
Console.WriteLine(s1 == s2);
Console.WriteLine("s2={0},s1={1}",s2,s1);
Console.WriteLine((object)s2==(object)s1);
*/
/*
#region//类型转换示例================================================
int i = 1015;
long l = i;//隐式转换类型,只能用在低类型向高类型转换上
//float f1 = 10.15;//float类型的要在后加F,不加F的话默认为Double型
float f = 10.15F;//单精度浮点数
int j =(int) f;//显示转换
System.Console.WriteLine("长整型l的值为:{0}",l);
System.Console.WriteLine("(int) {0}={1}",f,j );
Console.WriteLine("将整型i转化为字符串:" + i.ToString());
#endregion
*/
// 字符串示例,其中string和String是相同的,根据喜好使用=======================================
/*
string s1 = null;
String s2 = s1;
String s3 = "";
string s4 = String.Empty;
Console.WriteLine(s2==s3);//输出结果为False
Console.WriteLine(s2 == s4);//输出结果为False
Console.WriteLine(s3 == s4);//输出结果为True;
//以上的s2赋值为null,s3,s4的赋值为空(其中String.Empty为""的替代方法)。
//且由以上输出结果可知null与""是不同的。
const string s5="I'm a chinese";//定义一个固定的字符串,此后不能将其它字符串赋值给s5
//s5 = "aaa";这条语句将产生错误
//不过不加const的化也不允许对字符串重新赋值,如下也为错误定义:
//string s7="USA";
//string s7="UAE";不能如此,提示“已在刺范围定义了名为‘s5’的局部变量”。
//s7="UAE";可以重新给s7赋值,但"USA"并没有消失,只是s7现在指向了新的字符串对象"UAE",此时"USA"没被引用则在适当时将被回收
Console.WriteLine("s5为:"+s5);/
char[] c = {'a','b','c','d','e'};
string s6=new string (c);//从数组转化为字符串
Console.WriteLine("s6为:"+ s6);//输出结果为True;
*/
//当连接 s1 和 s2 的内容以形成一个字符串时,不会修改两个原始字符串。+= 运算符会创建一个包含组合内容的新字符串。这个新对象赋给变量 s1,而最初赋给 s1 的对象由于没有其他任何变量包含对它的引用而释放,用于垃圾回收。
/*
string s1 = "A string is more ";
string s2 = "than the sum of its chars.";
//s1 += s2;
s1 = s1 + s2;
System.Console.WriteLine(s1);
// Output: A string is more than the sum of its chars.
*/
string s="R\tU\tOK?";//转义字符\t
Console.WriteLine(s);
string filePath = @"C:\Users\scoleridge\Documents\";//原义字符串
Console.WriteLine(filePath);
string uu = @"a
b
c
d";
Console.WriteLine(uu);
string uu2 = @"你是""猪""";//要在字符串内输出双引号,则需套用"",即""""
Console.WriteLine(uu2);//输出你是"猪"
}
}
}
快速链接:http://www.diaomin.org/go/32933.html
Tags:




没人甩我?!
到此一游