C#面试题
一列数的规则如下: 1、1、2、3、5、8、13、21、34......求第30位数是多少,用递归算法实现。
答:public class MainClass
{
public static void Main()
{
Console.WriteLine(Foo(30));
}
public static int Foo(int i)
{
if (i <= 0)
return 0;
else if(i > 0 && i <= 2)
return 1;
else return Foo(i -1) + Foo(i - 2);
}
}
请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
答:
这个问题有两种答案一种是在WEB里答案如下
foreach (Control control in this.Form.Controls)
{
if (control is System.Web.UI.WebControls.TextBox)
{
TextBox tb = (TextBox)control;
tb.Text = String.Empty;
}
}
另一种是在FORM里
foreach (System.Windows.Forms.Control control in this.Controls)
{
if (control is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ;
tb.Text = String.Empty ;
}
}
public class Program
{
static void Main(string[] args)
{
A a = new C();
a.Show();
//输出为B 当为 B a = new C();的时候也是输出B,当为C的时候输出C
Console.ReadKey();
}
}
public class A
{
public virtual void Show()
{
Console.WriteLine("A");
}
}
public class B : A
{
public override void Show()
{
Console.WriteLine("B");
}
}
public class C : B
{
public new void Show()
{
Console.WriteLine("C");
}
}