01:在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别。
string str = null 是不给他分配内存空间
而string str = “”给它分配长度为空字符串的内存空间
02:简述类和结构的相同点和不同点。并用代码举例。
b
结构与类的不同:结构为值类型而类是引用类型,结构不支持继承,结构的值存储在堆栈上而类存储在堆上。
//定义类的语法
class Person
{
private string name;
private int age;
public void SayHi()
{
Console.WriteLine("Hello,My Name is "+this.name+",My Age is "+this.age);
}
}
//定义结构的语法
struct Rectangle
{
private int width;
private int height;
public int GetArea()
{
return this.width * height;
}
}
03:什么是拆箱和装箱?举例说明
拆箱:引用类型转换成值类型
装箱值类型转换成引用类型
例如:
bool a=true;
object b=a; //装箱
bool c=(bool)b; //拆箱
04:编程实现一个冒泡排序
int a = 0;
int[] num = new int[] { 19, 1, 38, 49, 85, 16, 867, 282 };
for (int i = 0; i < num.Length - 1; i++)
{
for (int j = 0; j < num.Length - 1 - i; j++)
{
if (num[j] > num[j + 1])
{
a = num[j];
num[j] = num[j + 1];
num[j + 1] = a;
}
}
}
foreach (int number in num)
{
Console.Write(number + " ");
}
05:编程实现一个递归方法
public static int F(int n)
{
int sum = 0;
if (0==n)
{
return 1;
}else{
sum = n * F(n - 1);
}
return sum;
}
06:说说目前学的集合有哪些?,每一种集合的特点以及使用场景
非泛型集合 :ArrayList,HashTable
泛型集合:Dictionary <K, V> ,List <T>
07:变量被标记为 “const” 和readonly” 有何不同?
const修饰的常量在编译期间就被解析,即常量值被替换成初始化的值;
readonly修饰的常量则延迟到运行的时候此外const常量既可以声明在类中也可以在函数体内,但是static readonly常量只能声明在类中。
08:“out” 和 “ref” 参数有何不同?用代码举例
out:
static void Main(string[] args)
{
string str = "初始化赋值";
MethodOut(out str);
Console.ReadKey();
}
public static void MethodOut(out string str)
{
str = "Hello World! ---out";
Console.WriteLine(str);
}
ref:
//ref参数的参数必须最先初始化,
static void Main(string[] args)
{
string str = "初始化赋值";
MethodRef(ref str);
Console.ReadKey();
}
public static void MethodRef(ref string str)
{
str = "Hello World! --Ref";
Console.WriteLine(str);
}
09:“StringBuilder” 和 “String” 有何不同?
String可以储存和操作字符串,即包含多个字符的字符数据。这个String类提供了存储数值不可改变的字符串。
StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义,这种情况只可能有一个线程访问它,不存在不安全的因素