01:在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别。
答:string str=null 把这个引用指向了一个null,没有地址没有值的地方。
string str="" 把这个引用指向了一个地址,地址里面存的是空的字符。
02:简述类和结构的相同点和不同点。并用代码举例。
答:相同点:1.都有属性和方法 2.和类一样可以继承自接口; 不同点:结构是值类型,类是引用类型;结构可以不用new,直接定义结构的对象即可;声明结构的对象后,必须给结构的成员赋初值
public class Person{
}
public struct Person{
}
03:什么是拆箱和装箱?举例说明
答:将值类型转为引用类型过成为装箱,反之为拆箱;
static void Main(string[] args)
{
int i = 123;
object o = i;//装箱
int j= (int)o;//拆箱
}
04:编程实现一个冒泡排序
static void Main(string[] args)
{
int temp = 0;
int[] arr = { 23, 44, 66, 76, 98, 11, 3, 9, 7 };
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
Console.WriteLine("排序后的数组:");
foreach (int item in arr)
{
Console.Write(item + " ");
}
Console.WriteLine();
Console.ReadKey();
}
05:编程实现一个递归方法
public int fun(int num){
int result=0;
if(num==1){
return 1;
}else{
result=fun(num-1)*num;
return result;
}
}
06:说说目前学的集合有哪些?,每一种集合的特点以及使用场景
答:ArrayList集合:可以存进去多个类型,用于初始化数据; Hashtable:可以存储任意的键值对类型。Directory键值对:只能存储特定的键值对类型,相比上面避免了频繁的拆箱和装箱。List泛型集合:只可以存储一种数据类型,相比上面也是避免了频繁的拆箱和装箱,提高了程序运行效率。
07:变量被标记为 “const” 和readonly” 有何不同?
答:const修饰的常量在声明的时候必须初始化;readonly修饰的常量则可以延迟到构造函数初始化
08:“out” 和 “ref” 参数有何不同?用代码举例
答:ref可以不赋初始值,但是out参数必须赋初始值
static void Main(string[] args)
{
string str = "初始化赋值";
MethodOut(out str);
Console.ReadKey();
}
static void Main(string[] args)
{
string str ;
MethodOut(out str);
Console.ReadKey();
}
09:“StringBuilder” 和 “String” 有何不同?
答:String类型在做字符串的连接操作时,效率是相当低的,并且由于每做一个连接操作,都会在内存中创建一个新的对象,占用了大量的内存空间。这样就引出StringBuilder对象,StringBuilder对象在做字符串连接操作时是在原来的字符串上进行修改,改善了性能。