C#经典面试题及答案
-
现在收集些常见面试题:
这里找了一个比较全的,提供给大家下载:Net面试题大全
将比较常见的C#面试题分布出来供有需求的朋友参考,本文章会不断更新。如果有好的面试题不妨给我留言,我来完善。最好是有答案的。
1:委托和事件
//事件类
public class EventClass
{
public void display(object sender, System.EventArgs e)
{
Console.WriteLine("This is the Event Class");
Console.ReadLine();
}
}
//调用类
class InvokeClass
{
//声明代理对象,注意参数
public delegate void delegateobj(object sender, System.EventArgs e);
//声明事件对象
private event delegateobj obj;
//声明要调用的事件类对象
private EventClass ec;
public InvokeClass()
{
ec = new EventClass();
//添加事件对象到事件队列中,参数为要调用的事件
this.obj += new delegateobj(ec.display);
}
//调用delegate对象触发事件
protected void OnObj(System.EventArgs e)
{
if (this.obj != null)
{
obj(this, e);
}
}
public void RaiseEvent()
{
EventArgs e = new EventArgs();
OnObj(e);
}
static void Main(string[] args)
{
InvokeClass ic = new InvokeClass();
Console.WriteLine("Please input a string");
string input = Console.ReadLine();
if (input.Equals(""))
{
Console.WriteLine("Sorry,you don't input anything");
}
else
{
//触发事件
ic.RaiseEvent();
}
}
}
2:遍历页面中所有的TextBox,交将值设置成"a"for ( int j = 0 ; j < this .Controls.Count; j ++ )
{
foreach (object o in Page.Controls[j].Controls)
{
if (o is TextBox)
{
TextBox txt = (System.Web.UI.WebControls.TextBox)o;
txt.Text = "A";
}
}
}
3:常用排序算法/// <summary>
/// /冒泡排序
/// </summary>
private void BubbleSort()
{
//冒泡排序
int[] list = new int[5] { 111, 12, 223, 854, -5655 };//初始化数组
int i, j, temp;
for (j = 1; j < list.