1:在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别。
string str= " ",表示一个空串,被实列化了,占用了内存空间,string str=null,表示一个空引用,没有占用了空间
02:简述类和结构的相同点和不同点。并用代码举例。
//结构
public struct Demo
{
}
//类
public class Demo
{
}
3:什么是拆箱和装箱?举例说明
拆箱是把“引用”类型转换成“值”类型;装箱是把“值”类型转换成“引用类型”
4:编程实现一个冒泡排序
int [] a={1,8,6,9,5,4,2};
for (int i = 0; i < a.length; i++) {
for (int j = i+1; j < a.length; j++) {
int num=0;
if (a[i]>a[j]) {
num=a[j];
a[j]=a[i];
a[i]=num;
}
}
}
5:编程实现一个递归方法
public int num1(int d)
{
if (d==1||d==2)
{
return d;
}
return num1(d - 2) + num1(d - 1);
}
6:说说目前学的集合有哪些?,每一种集合的特点以及使用场景
ArrayList:可以存放多种类型进去,类型不固定的时候。
List<T>:只可以存储一种数据类型,相比上面也是避免了频繁的拆箱和装箱,提高了程序运行效率,类型固定的时候。
Hashtable:可以存储任意的键值对类型,类型不固定的时候。
Dictionary<k,v>:只能存储特定的键值对类型,相比上面避免了频繁的拆箱和装箱,类型固定的时候。
7:常量被标记为 “const” 和readonly” 有何不同?
const 字段只能在该字段的声明中初始化。 readonly 字段可以在声明或构造函数中初始化。
8:“out” 和 “ref” 参数有何不同?用代码举例
string str;// = "初始化赋值";
MethodOut(out str);//可以不赋初始值
MethodRef(ref str);//需赋初始值
Console.ReadKey();
}
public static void MethodOut(out string str)
{
str = "Hello World! ---out";
Console.WriteLine(str);
}
public static void MethodRef(ref string str)
{
str = "Hello World! ---ref";
Console.WriteLine(str);
}
09:“StringBuilder” 和 “String” 有何不同?
StringBuilder不需要每次都去分配内存空间,当我们需要多次的对一个字符串进行多次操作的时候,他的效率要远远 高 与string。