01:在C#中,string str = null 与 string str = “” 请尽量使用文字或图
象说明其中的区别。
string str= " ",表示一个空串,被实列化了,占用了内存空间,
string str=null,表示一个空引用,没有占用了空间,
02:简述类和结构的相同点和不同点。并用代码举例。
类可以被继承,而结构则不支持。
结构对象不能像类对象一样赋值为null。
结构不能像类一样定义析构器。
结构不能像类一样定义为抽象的。
在结构中不能重写方法,除非是object类型的如下方法:
- Equals()
- GetHashCode()
- GetType()
- ToString()
若要让结构具有多态特性,可以让其实现接口。
在类中定义的事件是线程安全的,而结构则不是。
结构总是具有一个默认的公共无参构造函数,但去不能像类一样定义私有的无参构造函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
}
}
}
03:什么是拆箱和装箱?举例说明
static void Main(string[] args){
int i=123;
object o=i;//装箱
int j=(int)o;//拆箱
04:编程实现一个冒泡排序
int[] score = new int[5];
int i, j;
int temp;
Console.WriteLine("请输入五位学生的成绩:");
for ( i = 0; i < score.Length; i++)
{
Console.WriteLine("请输入第{0}个学生的成绩:",i+1);
score[i] = int.Parse(Console.ReadLine());
}
for ( i = 0; i < score.Length-1; i++)
{
for ( j = 0; j < score.Length-1-i; j++)
{
if (score[j]>score[j+1])
{
temp = score[j];
score[j] = score[j+1];
score[j + 1] = temp;
}
}
}
Console.WriteLine("排序后的成绩为:");
for ( i = 0; i < score.Length; i++)
{
Console.WriteLine("{0}\t",score[i]);
}
Console.ReadLine();
}
05:编程实现一个递归方法
public int Function(int num)
{
if (num == 1)
return num;
return Function(num - 1) * num;
}
06:说说目前学的集合有哪些?,每一种集合的特点以及使用场景
ArrayList,List,Hashtable,Dictionary<key,value>
ArrayList和List获取,删除元素是通过下标或对象名获取;
Hashtable,和Dictionary<key,value>获取,删除元素是通过key获取。
List和Dictionary<key,value>访问元素无须转换。
07:变量被标记为 “const” 和readonly” 有何不同?
const修饰的常量在声明的时候必须初始化;readonly修饰的常量则可以延迟到构造函数初始化 ;
08:“out” 和 “ref” 参数有何不同?用代码举例
ref是引用把值类型转换成弓用,out是输出,区别是ref是输入兼输出的,out只是单纯的输出
public void num(ref int num1, ref int num2){ }
public void num(out int num1, out int num2){ }
09:“StringBuilder” 和 “String” 有何不同?
用string += 追加的时候会分配新的地址,StringBuilder不会 ,所以StringBuilder的运行速度会快,不会浪费资源空间。