新蛋科技.net工程方面的笔试题
新蛋
1、 DataSet和DataReader的区别和相同点,分别适合用于什么样的情况?
答:
2、 有基类如下:
public class parent
{
public parent()
{
Console.Write(“Parent”);
}
}
请写出一个子类Son,要求子类的构造函数实现如下的功能:(1)输出儿子的NAME、SEX、AGE、以及Parent,(2)要求在Son的构造函数里不能有任何的命令语句出现。
public class parent
{
public parent()
{
Console.Write(“Parent”);
}
}
public class Son:parent
{ static string name=null;
static int sex=0;
static int age=0;
public parent(string name,int sex,int age):base()
{
name=name;
sex=sex;
age=age;
display();
}
publci void display()
{
Console.WriteLine(“name=”+name);
Console.WriteLine(“sex=”+sex);
Console.WriteLine(“age=”+age);
}
}
3、 请例举出三种以上进行页面重定向的方式(包括服务端和客户端)。
答: 第一种: Response.Redirect,
第二种: Server.Transfer
第三种: <a href = “url”></a>
function redirect(url) {
document.theForm.action = url;
document.theForm.submit();
}
第四种: StringBuilder sb=new StringBuilder();
sb.Append(“<script language=javascript>parent.frames[\"main\"].location=\”");
sb.Append(“GetResult.aspx”);
sb.Append(“?minVal=”); sb.Append(TextBox1.Text);
sb.Append(“&maxVal=”); sb.Append(TextBox2.Text);
sb.Append(“\”;</script>”);
Response.Write(sb.ToString());
4、 写出禁用ViewState的语句。
答: Control(具体的某个控件).EnableViewState=false;
5、 请谈一谈.NET的code-behind模式和code_clude模式的区别,和各自的优点及缺点。
6、 写出下列七个程序段的输出结果:
(1)
interface InFace
{
void fun();
}
class MyParent:InFace
{
public void fun()
{
Console.WriteLine(“Parent”);
}
}
class MySon:MyParent
{
public void fun()
{
Console.WriteLine(“Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
InFace inf=new MySon();
inf.fun();
}
}
结果:Parent
(2)
interface InFace
{
void fun();
}
class MyParent:InFace
{
public new void fun()
{
Console.WriteLine(“Parent”);
}
}
class MySon:MyParent
{
public void fun()
{
Console.WriteLine(“Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
InFace inf=new MySon();
inf.fun();
Console.Read();
}
}
结果:Parent
(3)
interface InFace
{
void fun();
}
class MyParent:InFace
{
public new void fun()
{
Console.WriteLine(“Parent”);
}
}
class MySon:MyParent
{
public new void fun()
{
Console.WriteLine(“Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
InFace inf=new MySon();
inf.fun();
Console.Read();
}
}
结果:Parent
(4)
interface InFace
{
void fun();
}
class MyParent:InFace
{
public void fun()
{
Console.WriteLine(“Parent”);
}
}
class MySon:MyParent
{
public override void fun()
{
Console.WriteLine(“Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
InFace inf=new MySon();
inf.fun();
Console.Read();
}
}
结果:语法错误: 无法重写继承成员“ConsoleApplication6.MyParent.fun()”,因为它未标记为 virtual、abstract 或 override
(5)
interface InFace
{
void fun();
}
abstract class MyParent:InFace
{
public virtual void fun()
{
Console.WriteLine(“Parent”);
}
}
class MySon:MyParent
{
public override void fun()
{
Console.WriteLine(“Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
InFace inf=new MySon();
inf.fun();
Console.Read();
}
}
结果:Son
(6)
interface InFace
{
void fun();
}
class MyParent:InFace
{
public virtual void fun()
{
Console.WriteLine(“Parent”);
}
}
class MySon:MyParent
{
public override void fun()
{
Console.WriteLine(“Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
InFace inf=new MySon();
inf.fun();
Console.Read();
}
}
结果:Son
(7)
interface InFace
{
void fun();
}
abstract class MyParent:InFace
{
public void fun()
{
Console.WriteLine(“Parent”);
}
}
class MySon:MyParent
{
public override void fun()
{
Console.WriteLine(“Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
InFace inf=new MySon();
inf.fun();
Console.Read();
}
}
结果:语法错误: 无法重写继承成员“ConsoleApplication6.MyParent.fun()”,因为它未标记为 virtual、abstract 或 override
8、在.NET中有自动的垃圾回收机制,但是我们也可以显示声明类的析构函数,请写出下列程序的输出结果:像这个程序一样我们显示声明类的析构函数,会有什么问题出现?
class Parent
{
public Parent()
{
Console.WriteLine(“Parent”);
}
~Parent()
{
Console.WriteLine(“Delete Parent”);
}
}
class Son:Parent
{
public Son():base()
{
Console.WriteLine(“Son”);
}
~Son()
{
Console.WriteLine(“Delete Son”);
}
}
public class MyTest
{
public static void Main(string[] args)
{
Son son=new Son();
}
}
结果:Parent
Son
Delete Son
Delete Parent
9、 按值传递和按引用传递各有什么特点。它们有什么区别?
答:在按值传递中,在被调方法中对变量所做的修改不会影响主调方法中的变量。
在按引用传递中,在被调方法中对变量所做的修改会反映到主调方法中的变量。
10、 写出下更程序的输出结果:
(1)public class MyTest
{
public static void Main(string[] args)
{
int i=10;
fun(i);
Console.WriteLine(“i=”+i);
Console.Read();
}
public static int fun(int a)
{
a++;
Console.WriteLine(“a=”+a);
return a;
}
}
结果:a=11
i=10
(2)
public static void Main(string[] args)
{
int i=10;
fun(out i);
Console.WriteLine(“i=”+i);
Console.Read();
}
public static int fun(out int a)
{
a++;
Console.WriteLine(“a=”+a);
return a;
}
结果:语法错误: 控制离开当前方法之前必须对输出参数“a”赋值
使用了未赋值的局部变量“a”
(3)
public class MyTest
{
public static void Main(string[] args)
{
int i=10;
fun(out i);
Console.WriteLine(“i=”+i);
Console.Read();
}
public static int fun(out int a)
{
a=12;
a++;
Console.WriteLine(“a=”+a);
return a;
}
}
结果:a=13
i=13
(5)
public class MyTest
{
public static void Main(string[] args)
{
int i=10;
fun(ref i);
Console.WriteLine(“i=”+i);
Console.Read();
}
public static int fun(ref int a)
{
a++;
Console.WriteLine(“a=”+a);
return a;
}
}
结果:a=11
i=11
附关于out参数的相关知识点:
必须被赋值。
方法参数上的 out 方法参数关键字使方法引用传递到方法的同一个变量。当控制传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。
当希望方法返回多个值时,声明 out 方法非常有用。使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。
若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。
不必初始化作为 out 参数传递的变量。然而,必须在方法返回之前为 out 参数赋值。
属性不是变量,不能作为 out 参数传递。
如果两个方法的声明仅在 out 的使用方面不同,则会发生重载。不过,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的:
class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(out int i) {i = 10;}
}
而以下重载声明是无效的:
class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
与所有的 out 参数一样,在使用数组类型的 out 参数前必须先为其赋值,即必须由接受方为其赋值。例如:
public static void MyMethod(out int[] arr)
{
arr = new int[10]; // definite assignment of arr
}
与所有的 ref 参数一样,数组类型的 ref 参数必须由调用方明确赋值。因此不需要由接受方明确赋值。可以将数组类型的 ref 参数更改为调用的结果。例如,可以为数组赋以 null 值,或将其初始化为另一个数组。例如:
public static void MyMethod(ref int[] arr)
{
arr = new int[10]; // arr initialized to a different array
}
下面的两个示例说明 out 和 ref 在将数组传递给方法上的用法差异。
示例 1
在此例中,在调用方(Main 方法)中声明数组 myArray,并在 FillArray 方法中初始化此数组。然后将数组元素返回调用方并显示。
// cs_array_ref_and_out.cs
using System;
class TestOut
{
static public void FillArray(out int[] myArray)
{
// Initialize the array:
myArray = new int[5] {1, 2, 3, 4, 5};
}
static public void Main()
{
int[] myArray; // Initialization is not required
// Pass the array to the callee using out:
FillArray(out myArray);
// Display the array elements:
Console.WriteLine(“Array elements are:”);
for (int i=0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
输出
Array elements are:
1
2
3
4
5
示例 2
在此例中,在调用方(Main 方法)中初始化数组 myArray,并通过使用 ref 参数将其传递给 FillArray 方法。在 FillArray 方法中更新某些数组元素。然后将数组元素返回调用方并显示。
// cs_array_ref_and_out2.cs
using System;
class TestRef
{
public static void FillArray(ref int[] arr)
{
// Create the array on demand:
if (arr == null)
arr = new int[10];
// Otherwise fill the array:
arr[0] = 123;
arr[4] = 1024;
}
static public void Main ()
{
// Initialize the array:
int[] myArray = {1,2,3,4,5};
// Pass the array using ref:
FillArray(ref myArray);
// Display the updated array:
Console.WriteLine(“Array elements are:”);
for (int i = 0; i < myArray.Length; i++)
Console.WriteLine(myArray[i]);
}
}
输出
Array elements are:
123
2
3
4
1024
10、 怎样从弹出窗口中刷新主窗口?
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = new Form2();
try
{
frm.ShowDialog(this);
}
finally
{
frm.Dispose();
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Form parent = this.Owner as Form;
if(parent != null)
parent.Refresh();
}
11、 Attribute的参数?
答:Attribute类的构造函数没有参数,
AttributeUsageAttribute类指定另一特性类的用法,有一个参数
public AttributeUsageAttribute( AttributeTargets validOn);
12、 怎样确定垃圾确实被回收了,调用了supressfinalize或collect方法就一定销毁了对象吗?显示调用了析构方法就一定销毁了对象吗?
答:垃圾回收 GC 类提供 GC.Collect 方法,您可以使用该方法让应用程序在一定程度上直接控制垃圾回收器,这就是强制垃圾回收。
Finalize 方法和析构函数如何允许对象在垃圾回收器自动回收对象的内存之前执行必要的清理操作。
对于您的应用程序创建的大多数对象,可以依靠 .NET Framework 的垃圾回收器隐式地执行所有必要的内存管理任务。但是,在您创建封装非托管资源的对象时,当您在应用程序中使用完这些非托管资源之后,您必须显式地释放它们。最常见的一类非托管资源就是包装操作系统资源的对象,例如文件、窗口或网络连接。虽然垃圾回收器可以跟踪封装非托管资源的对象的生存期,但它不了解具体如何清理这些资源。对于这些类型的对象,.NET Framework 提供 Object.Finalize 方法,它允许对象在垃圾回收器回收该对象使用的内存时适当清理其非托管资源。默认情况下,Finalize 方法不执行任何操作。如果您要让垃圾回收器在回收对象的内存之前对对象执行清理操作,您必须在类中重写 Finalize 方法。当使用 C# 和 C++ 的托管扩展以外的编程语言进行开发时,您可以实现 Finalize 方法。C# 和托管扩展提供析构函数作为编写终止代码的简化机制。析构函数自动生成 Finalize 方法和对基类的 Finalize 方法的调用。在 C# 和托管扩展编程语言中,您必须为终止代码使用析构函数语法
13、 怎样进行Forms的身份验证
就是考 FormsAuthentication
Web。Config文件设置如下:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<system.web>
<!– 身份验证
此节设置应用程序的身份验证策略。可能的模式是 “Windows”、
“Forms”、 “Passport” 和 “None”
“None” 不执行身份验证。
“Windows” IIS 根据应用程序的设置执行身份验证
(基本、简要或集成 Windows)。在 IIS 中必须禁用匿名访问。
“Forms” 您为用户提供一个输入凭据的自定义窗体(Web 页),然后
在您的应用程序中验证他们的身份。用户凭据标记存储在 Cookie 中。
“Passport” 身份验证是通过 Microsoft 的集中身份验证服务执行的,
它为成员站点提供单独登录和核心配置文件服务。
–>
<authentication mode=”Forms” >
<forms name=”FrmMain” loginUrl=”PageLogin.aspx” protection=”None” timeout=”60″>
<credentials passwordFormat=”Clear”>
<user name=”User1″ password=”user1!”/>
<user name=”User2″ password=”user2@”/>
<user name=”User3″ password=”user3#”/>
</credentials>
</forms>
</authentication>
<!– 授权
此节设置应用程序的授权策略。可以允许或拒绝不同的用户或角色访问
应用程序资源。通配符: “*” 表示任何人,”?” 表示匿名
(未经身份验证的)用户。
–>
<authorization>
<allow users=”user2,user3″ /> <!– 允许所有用户 –>
<deny user=”user1″/><!–否定的用户列表–>
<!– <allow users=”[逗号分隔的用户列表]”
roles=”[逗号分隔的角色列表]“/>
<deny users=”[逗号分隔的用户列表]”
roles=”[逗号分隔的角色列表]“/>
–>
</authorization>
<!– 会话状态设置
默认情况下,ASP.NET 使用 Cookie 来标识哪些请求属于特定的会话。
如果 Cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。
若要禁用 Cookie,请设置 sessionState cookieless=”true”。
–>
<sessionState
mode=”InProc”
stateConnectionString=”tcpip=127.0.0.1:42424″
sqlConnectionString=”data source=127.0.0.1;Trusted_Connection=yes”
cookieless=”false”
timeout=”20″
/>
<!– 全球化
此节设置应用程序的全球化设置。
–>
</system.web>
</configuration>
14、 Thread中的Sleep与Suspend有什么区别,Abort的用法
Sleep //定期休眠
Suspend //暂停直到被唤醒
Abort //强制杀死线程(不建议使用)
15、 定义一个有索引器的类,并能枚举
public class MyArray
{
private string innerArray[];
public MyArray(int length)
{
this.innerArray = new string[length];
}
public string this[int index]
{
get
{
return this.innerArray[index];
}
set
{
this.innerArray[index] = value;
}
}
}
16、 SessionState的状态值:InProc?、On、Off、SQL Server?
为当前应用程序配置会话状态设置。
<sessionState mode=”Off|InProc|StateServer|SQLServer”
cookieless=”true|false”
timeout=”number of minutes”
stateConnectionString=”tcpip=server:port”
sqlConnectionString=”sql connection string”
stateNetworkTimeout=”number of seconds”/>
17、 HttpHandler
18、 StringBuilder与“+”的本质区别
StringBuilder // StringBuilder是可变的
String + string = AnotherString //字符串不可变,+会返回一个全新的字符串
19、 强指针与弱指针
强引用被C#语法直接支持,只要有强引用指向对象,一定不回收
弱引用被WeakReference类支持,当对象被回收时,指向对象的所有弱引用置空
