1.在c#中string str=null与string str="“请尽量使用文字或图像说明其中的区别
答:string str=null:表示一个空引用,没有占用了空间
string str=”":表示一个空串,被实例化了,占用了内存空间
2.简述类和结构的相同点和不同点,并用代码举例
答:不同点:语法:类:
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 Person
{
private int weith;
private int height;
public int GetArea()
{
return this.weith * height;
}
}
相同点:都是创建对象的模板,每个对象都包含数据,并提供了处理和访问数的方法
结构:
public struct Person
{
public int id;
public int age;
public void Show()
{
Console.WriteLine("ID:{0}\n年龄:{1}",id,age);
}
}
static void Main(string[] args)
{
Person person;
person.id = 101;
person.age = 21;
person.Show();
Console.ReadLine();
}
类:
public class Person
{
public int Id { get; set; }
public int Age { get; set; }
public Person(int id,int age)
{
Id = id;
Age = age;
}
public void Show()
{
Console.WriteLine("ID:{0}\n年龄:{1}",this.Id,this.Age);
}
}
static void Main(string[] args)
{
Person person = new Person(101, 21);
person.Show();
Console.ReadLine();
}
3.什么是装箱和拆箱,举例说明
答:装箱:值类型转换为引用类型
static void Main(string[] args)
{
int i = 123;
object o = i;
i = 456;
Console.WriteLine("值类型为:{0}",i);
Console.WriteLine("引用类型为:{0}", o);
Console.ReadLine();
}
拆箱:引用类型转换为值类型
static void Main(string[] args)
{
int i = 123;
object o = i;
int j = (int)o;
Console.WriteLine("值类型为:{0}",i);
Console.WriteLine("引用类型为:{0}", o);
Console.ReadLine();
}
4.编程实现一个冒泡排序
答:
static void Main(string[] args)
{
int[] array = new int[3];
int teep=0;
for (int i = 0; i <array.Length-1; i++)
{
Console.WriteLine("");
for (int j= 0; j < array.Length; j++)
{
Console.WriteLine("*");
if (array[j]<array[i])
{
teep=array[i];
array[i]=array[j];
array[j]=teep;
}
}
}
Console.ReadLine();
}
5.编程实现一个递归方法
答:
public double CC(int n)
{
if (n==0||n==1)
{
return 1;
}
else
{
return n * CC(n - 1);
}
}
6.说说目前学的集合有哪些?,每一种集合的特点以及使用场景
答:ArrayList:可以动态维护,可以根据需要自动扩充
Hashtable:不能通过索引访问,只能通过Key访问Value
List:访问元素无须进行类型转换
Dictionary<k,v>:具有泛型的全部特性,编译时检查类型约束,获取元素时无须类型转换
7.变量被标记为 “const” 和readonly” 有何不同?
答:const:修饰的常量在声明的时候必须初始化
readonly:修饰的常量则可以延迟到构造函数初始化
8.“out” 和 “ref” 参数有何不同?用代码举例
答:ref和out关键字在运行时处理方式不同
class HH
{
public void SS(out int i) { };
public void SS(ref int i) { };
}
9.“StringBuilder” 和 “String” 有何不同?
答:string是final类,stringBuilder不是,string对象是恒不变的,stringBuilder对象是表示字符串是可变的