01:在C#中,string str = null 与 string str = “” 请尽量使用文字或图
象说明其中的区别。
sting str=null:没有开辟空间,值为空
string str = “” :开辟空间,值为空
02:简述类和结构的相同点和不同点。并用代码举例。
namespace 类对象
{
//枚举
public enum Gender
{
男,
女
}
//结构体
public struct stuPerson
{
public string stuName;
public int stuAge;
public Gender stuSex;
public void stuSayHello()
{
Console.WriteLine("我是{0},年龄{1},性别{2}", stuName, stuAge, stuSex);
}
//必须定义有参数的构造函数
public stuPerson(string name, int age, Gender sex)
{
this.stuName = name;
this.stuAge = age;
this.stuSex = sex;
}
}
class Program
{
static void Main(string[] args)
{
//实例化类
Person Liuxiang = new Person(); //无参数的构造函数实例化的对象
Liuxiang.Name = "刘翔";
Liuxiang.Age = 30;
Liuxiang.Sex = Gender.男;
Liuxiang.SayHello();
//声明另一个实例
Person LXSon = Liuxiang;
LXSon.Age = 10;
//查看类 LiuXiang 和 LXSon中字段的值
Console.WriteLine("LiuXiang 年龄{0}", Liuxiang.Age.ToString()); //10
Console.WriteLine("LXSon 年龄{0}", LXSon.Age.ToString()); //10
Console.WriteLine();
//结构体
stuPerson YaoMing = new stuPerson("姚明",33,Gender.男);
YaoMing.stuSayHello();
stuPerson YMSon = YaoMing;
YMSon.stuAge = 13;
//查看结构体中 YaoMing 和 YMSon中字段的值
Console.WriteLine("YaoMing 年龄{0}", YaoMing.stuAge.ToString()); //10
Console.WriteLine("YMSon 年龄{0}", YMSon.stuAge.ToString()); //10
Console.ReadLine();
}
}
class Person
{
//定义字段
public string Name;
public int Age;
public Gender Sex;
//定义方法
public void SayHello()
{
Console.WriteLine("我是{0},年龄{1},性别{2}", this.Name, this.Age, this.Sex);
}
//无参数的构造函数
public Person()
{
}
//有参数的构造函数
public Person(string name, int age, Gender sex)
{
this.Name = name;
this.Age = age;
this.Sex = sex;
}
}
}
结构是值类型,类则是引用类型。因此前者是放在栈(Stack)里,后者则仅仅是将引用地址存放在栈里,而具体的值则存放在堆(heap)里。如
03:什么是拆箱和装箱?举例说明
C#里那么叫
装箱:低类型–>高类型例:
objecti="aa";
这个是string–>object叫装箱
拆箱:高类型–>低类型例:
Stringi=(String)object;
这个是string–>int叫拆箱
04:编程实现一个冒泡排序
namespace test11
{
class Program
{
const int N = 10;
static void Main(string[] args)
{
int[] a=new int[N];
for (int i = 0; i < N; i++)
a[i] =Convert.ToInt32( Console.ReadLine());
for(int i=0;i<N-1;i++)
for (int j = N - 2; j >= i; j--)
{
if (a[j+1] < a[j])
{
int t;
t = a[j+1];
a[j+1] = a[j];
a[j] = t;
}
}
for (int i = 0; i < N; i++)
Console.WriteLine("{0}", a[i]);
}
}
}
05:编程实现一个递归方法
namespace zd3
{
class Program
{
public static double frac(int n)
{
if (n == 1)
return 1;
else
return n * frac(n - 1);
}
static void Main(string[] args)
{
Console.Write(frac(6));
}
}
}
06:说说目前学的集合有哪些?,每一种集合的特点以及使用场景
HashTable键值对:可以存储任意的键值对类型。
Directory键值对:只能存储特定的键值对类型,相比上面避免了频繁的拆箱和装箱。
ArrayList可以存放多种类型进去
List泛型集合:只可以存储一种数据类型,相比上面也是避免了频繁的拆箱和装箱,提高了程序运行效率。
07:变量被标记为 “const” 和readonly” 有何不同?
readonly为运行时常量,程序运行时进行赋值,赋值完成后便无法更改,因此也有人称其为只读变量。
const为编译时常量,程序编译时将对常量值进行解析,并将所有常量引用替换为相应值。
08:“out” 和 “ref” 参数有何不同?用代码举例
public void foo(ref int r, out int o)
{
if (o == 1)... // 错误,不能使用out的参数,因为o可能会被自动清空
if (r == 0) // 读取r的值
o = 0; // 设置o的值
else
o = -r;
r = 0; // 设置r的值
}
public void Test()
{
int r, o;
r = 0;
foo(ref r, out o);
}
09:“StringBuilder” 和 “String” 有何不同?
//最大的区别体现在字符串拼接的时候
string str1="hello ";
string str2="world";
str1 += str2;
//此时内存中存在三个对象分别为str1("hello world"), str2("hello"),以及一个没有任何引用的垃圾
//对象"hello"(该对象,最终会被GC收集,清理)
StringBuilder sb=new StringBuilder();
sb.Append("hello ");
sb.Append("world");
//此时内存中只有sb("hello world")一个对象
//所以在做大规模的字符串拼接的时候(比较常见的是ADO.NET中查询语句的拼接),推荐使用
//StringBuilder而不是string,因为用string的话,你的内存将会十分可怕,不要太过依赖GC,因为GC
//并不是实时的,不是说C#程序员就可以完全不管内存,能控制的还是要尽量控制.