.net面试题收集(四)
来源:互联网 发布:淘宝号怎么升级3心 编辑:程序博客网 时间:2024/06/15 20:25
1. 描述一下C#中索引器的实现过程,是否只能根据数字进行索引?
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
class SampleCollection<T>
{
private T[] arr = new T[100];
public T this[int i]
{
get{
return arr[i];
}
set{
arr[i] = value;
}
}
}// This class shows how client code uses the indexer
class Program
{
static void Main(string[] args){
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[0]);
}
}
{
private T[] arr = new T[100];
public T this[int i]
{
get{
return arr[i];
}
set{
arr[i] = value;
}
}
}// This class shows how client code uses the indexer
class Program
{
static void Main(string[] args){
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[0]);
}
}
2. 下面的例子中
using System;
class A
{
public static int X;
static A(){
X=B.Y+1;
}
}
class B
{
public static int Y=A.X+1;
static B(){}
class A
{
public static int X;
static A(){
X=B.Y+1;
}
}
class B
{
public static int Y=A.X+1;
static B(){}
static void Main(){ //主函数为程序的入口
Console.WriteLine("X={0},Y={1}",A.X,B.Y);
}
}
Console.WriteLine("X={0},Y={1}",A.X,B.Y);
}
}
产生的输出结果是什么? 答:x=1,y=2
3. 常用的调用web service方法有哪些?
答:访问WebService的三种方法,GET ,POST ,和基于SOAP协议的Web代理的调用
答:访问WebService的三种方法,GET ,POST ,和基于SOAP协议的Web代理的调用
4. 大概描述一下ASP。NET页面的生命周期
阶段
说明
页请求
页请求发生在页生命周期开始之前。用户请求页时,ASP.NET 将确定是否需要分析和编译页(从而开始页的生命周期),或者是否可以在不运行页的情况下发送页的缓存版本以进行响应。
开始
在开始阶段,将设置页属性,如 Request 和 Response。在此阶段,页还将确定请求是回发请求还是新请求,并设置 IsPostBack 属性。此外,在开始阶段期间,还将设置页的 UICulture 属性。
页初始化
页初始化期间,可以使用页中的控件,并将设置每个控件的 UniqueID 属性。此外,任何主题都将应用于页。如果当前请求是回发请求,则回发数据尚未加载,并且控件属性值尚未还原为视图状态中的值。
加载
加载期间,如果当前请求是回发请求,则将使用从视图状态和控件状态恢复的信息加载控件属性。
验证
在验证期间,将调用所有验证程序控件的 Validate 方法,此方法将设置各个验证程序控件和页的 IsValid 属性。
回发事件处理
如果请求是回发请求,则将调用所有事件处理程序。
呈现
在呈现期间,视图状态将被保存到页,然后页将调用每个控件,以将其呈现的输出提供给页的 Response 属性的 OutputStream。
卸载
完全呈现页、将页发送至客户端并准备丢弃时,将调用卸载。此时,将卸载页属性(如 Response 和 Request)并执行清理。
5. 在下面的例子里
using System;
class A
{
public A(){
PrintFields();
}
public virtual void PrintFields(){}
}
class B:A
{
int x=1;
int y;
public B(){
y=-1;
}
public override void PrintFields(){
Console.WriteLine("x={0},y={1}",x,y);
}
}
class A
{
public A(){
PrintFields();
}
public virtual void PrintFields(){}
}
class B:A
{
int x=1;
int y;
public B(){
y=-1;
}
public override void PrintFields(){
Console.WriteLine("x={0},y={1}",x,y);
}
}
当使用new B()创建B的实例时,产生什么输出?
答:x=1,y=0
答:x=1,y=0
6. 如何通过ADO.NET读取数据库中的图片?
// Assumes that connection is a valid SqlConnection object.
SqlCommand command = new SqlCommand("SELECT pub_id, logo FROM pub_info", connection);
// Writes the BLOB to a file (*.bmp).
FileStream stream;
// Streams the BLOB to the FileStream object.
BinaryWriter writer;
// Size of the BLOB buffer.
int bufferSize = 100;
// The BLOB byte[] buffer to be filled by GetBytes.
byte[] outByte = new byte[bufferSize];
// The bytes returned from GetBytes.
long retval;
// The starting position in the BLOB output.
long startIndex = 0;
// The publisher id to use in the file name.
string pubID = "";
// Open the connection and read data into the DataReader.
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read()){
// Get the publisher id, which must occur before getting the logo.
pubID = reader.GetString(0);
// Create a file to hold the output.
stream = new FileStream("logo" + pubID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
writer = new BinaryWriter(stream);
// Reset the starting byte for the new BLOB.
startIndex = 0;
// Read bytes into outByte[] and retain the number of bytes returned.
retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
// Continue while there are bytes beyond the size of the buffer.
while (retval == bufferSize) {
writer.Write(outByte);
writer.Flush();
// Reposition start index to end of last buffer and fill buffer.
startIndex += bufferSize;
retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
}
// Write the remaining buffer.
writer.Write(outByte, 0, (int)retval - 1); writer.Flush();
// Close the output file.
writer.Close(); stream.Close();
}// Close the reader and the connection.
reader.Close();
SqlCommand command = new SqlCommand("SELECT pub_id, logo FROM pub_info", connection);
// Writes the BLOB to a file (*.bmp).
FileStream stream;
// Streams the BLOB to the FileStream object.
BinaryWriter writer;
// Size of the BLOB buffer.
int bufferSize = 100;
// The BLOB byte[] buffer to be filled by GetBytes.
byte[] outByte = new byte[bufferSize];
// The bytes returned from GetBytes.
long retval;
// The starting position in the BLOB output.
long startIndex = 0;
// The publisher id to use in the file name.
string pubID = "";
// Open the connection and read data into the DataReader.
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read()){
// Get the publisher id, which must occur before getting the logo.
pubID = reader.GetString(0);
// Create a file to hold the output.
stream = new FileStream("logo" + pubID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
writer = new BinaryWriter(stream);
// Reset the starting byte for the new BLOB.
startIndex = 0;
// Read bytes into outByte[] and retain the number of bytes returned.
retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
// Continue while there are bytes beyond the size of the buffer.
while (retval == bufferSize) {
writer.Write(outByte);
writer.Flush();
// Reposition start index to end of last buffer and fill buffer.
startIndex += bufferSize;
retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
}
// Write the remaining buffer.
writer.Write(outByte, 0, (int)retval - 1); writer.Flush();
// Close the output file.
writer.Close(); stream.Close();
}// Close the reader and the connection.
reader.Close();
connection.Close();
7. 请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
protected void Page_Load(object sender, EventArgs e) {
foreach (Control ctl in Page.Controls[0].Controls) {
if (ctl.GetType().Name == "TextBox") {
TextBox tb = new TextBox();
tb = (TextBox)this.FindControl(ctl.ID);
tb.Text = "";
}
}
}
foreach (Control ctl in Page.Controls[0].Controls) {
if (ctl.GetType().Name == "TextBox") {
TextBox tb = new TextBox();
tb = (TextBox)this.FindControl(ctl.ID);
tb.Text = "";
}
}
}
8. Remoting简介
.NET Remoting(下文简称Remoting)是一种可用于开发分布式应用程序的技术。其主要的结构,分为:远程对象、提供远程对象的远程服务器,以及可以访问何使用远程对象的客户端。这三个部分,可以分布于同一台计算机的同一个进程,或者是不同的进程,也可以是处于网络上的不同的计算机。Remoting技术最大的特点,就是对远程通信的过程进行了抽象和封装,使开发人员不必去处理底层通信的细节,而可以把重点放在对业务逻辑的处理上。而且Remoting的通信协议也比较灵活,可以使用多个通信协议、不同的数据格式类型,以及不同类型的序列化机制。在某些情况下,Remoting还允许你使用自定义的数据格式。
.NET Remoting(下文简称Remoting)是一种可用于开发分布式应用程序的技术。其主要的结构,分为:远程对象、提供远程对象的远程服务器,以及可以访问何使用远程对象的客户端。这三个部分,可以分布于同一台计算机的同一个进程,或者是不同的进程,也可以是处于网络上的不同的计算机。Remoting技术最大的特点,就是对远程通信的过程进行了抽象和封装,使开发人员不必去处理底层通信的细节,而可以把重点放在对业务逻辑的处理上。而且Remoting的通信协议也比较灵活,可以使用多个通信协议、不同的数据格式类型,以及不同类型的序列化机制。在某些情况下,Remoting还允许你使用自定义的数据格式。
9. 重载与覆盖的区别
1、方法的覆盖是子类和父类之间的关系,是垂直关系;方法的重载是同一个类中方法之间的关系,是水平关系。
2、覆盖只能由一个方法,或只能由一对方法产生关系;方法的重载是多个方法之间的关系。
3、覆盖要求参数列表相同;重载要求参数列表不同。
4、覆盖关系中,调用那个方法体,是根据对象的类型(对象对应存储空间类型)来决定;重载关系,是根据调用时的实参表与形参表来选择方法体的。
1、方法的覆盖是子类和父类之间的关系,是垂直关系;方法的重载是同一个类中方法之间的关系,是水平关系。
2、覆盖只能由一个方法,或只能由一对方法产生关系;方法的重载是多个方法之间的关系。
3、覆盖要求参数列表相同;重载要求参数列表不同。
4、覆盖关系中,调用那个方法体,是根据对象的类型(对象对应存储空间类型)来决定;重载关系,是根据调用时的实参表与形参表来选择方法体的。
10. 大概描述一下ASP。NET服务器控件的生命周期
阶段
控件需要执行的操作
初始化
初始化在传入 web 请求生命周期内所需的设置
加载视图状态
在此阶段结束时,就会自动填充控件的 viewstate 属性
处理回发数据
处理传入窗体数据,并相应地更新属性
加载
执行所有请求共有的操作,如设置数据库查询。此时,树中的服务器控件已创建并初始化、状态已还原并且窗体控件反映了客户端的数据
发送回发更改通知
引发更改事件以响应当前和以前回发之间的状态更改
处理回发事件
处理引起回发的客户端事件,并在服务器上引发相应的事件
预呈现
在呈现输出之前执行任何更新。可以保存在预呈现阶段对控件状态所做的更改,而在呈现阶段所对的更改则会丢失
保存状态
在此阶段后,自动将控件的 viewstate 属性保持到字符串对象中。此字符串对象被发送到客户端并作为隐藏变量发送回来
呈现
生成呈现给客户端的输出
处置
执行销毁控件前的所有最终清理操作。在此阶段必须释放对昂贵资源的引用,如数据库链接
卸载
执行销毁控件前的所有最终清理操作
- .net面试题收集(四)
- Net面试题收集
- .NET面试题收集
- .NET面试题收集
- .Net面试法宝(面试题收集)
- .NET面试题大收集(1)
- .net面试题收集(一)
- .net面试题收集(二)
- .net面试题收集(三)
- .NET面试题大收集(2)
- .NET面试题大收集(3)
- Net面试法宝(面试题收集)
- ios面试题收集(四)
- iOS面试题收集(四)
- asp.net面试题收集
- asp.net面试题收集
- asp.net面试题收集
- asp.net面试题收集
- 转JAVA、CORBA版HelloWorld程序
- jSeamless - 基于java的UI抽象
- .net面试题收集(三)
- 情有千千劫,有感于程序编码的质量(一)
- jsp+ajax
- .net面试题收集(四)
- Java 学习笔记 (9) - Java 事件机制 内部匿名类的应用
- 北京市国民经济和社会发展第十一个五年计划发展纲要
- datagrid里的checkbox单选
- 巧施妙计,改造“窝里横”男人
- 我想知道一些关于vxworks的知识
- 一种新的穿透防火墙的数据传输技术
- Xen Memory Management
- SSH简介及配置使用SSH登陆远程主机