NET 编程题
1.C#编写创建一个线程的代码
using System;
using System.IO;
using System.Threading ;
class MyThread{
public int count;
string thrdName;
public MyThread(string name){
count=0;
thrdName=name;
}
public void run(){
Console.WriteLine(thrdName+”starting.”);
do
{
Thread.Sleep(500);
Console.WriteLine(“In”+thrdName+”, count is “+count);
count++;
}
while(count<10);
Console.WriteLine(thrdName+” terminating.”);
}
}
class MultiThread{
public static void Main()
{
Console.WriteLine(“Main thread starting.”);
MyThread mt=new MyThread(“Child #1″);
Thread newThrd=new Thread(new ThreadStart(mt.run));
newThrd.Start();
do {
Console.Write(“.”);
Thread.Sleep(100);
}
while(mt.count!=10);
Console.WriteLine(“Main thread ending.”);
}
}
2.写出一个函数来实现去掉任意一个给定的XML文档的重复结点,以给定的”key”重复作为结点重复的标准,
如下例:Milk 4Milk 0.5Coffe 0.5
当以Name字段作为key时,第一个和第二个结点是重复的,当Amount作为key时,则第二个和第三个结点是重复结点。
所以函数应该是这样格式的:
string DeDup(string xml, string keyNode, string rootPath)
参考解决方案:
private static string RemoveDuplicates(string xml, string key, string rootXPath)
{
XmlDocument doc = new XmlDocument();
List sb = new List();
string keyValue;
try
{
doc.Load(xml);
XmlElement root = doc.DocumentElement;
XmlNodeList xnodelist = root.SelectNodes(rootXPath);
int i=0;
foreach (XmlNode item in xnodelist)
{
Console.WriteLine(i + "\b\b\b\b\b");
i++;
keyValue = item.SelectSingleNode(key).InnerXml;
if (sb.Contains(keyValue))
xnode.RemoveChild(item);
else
sb.Add(keyValue);
}
return doc.OuterXml;
}
catch (Exception ex)
{
// Log exception...
throw ex;
}
}
3.如何在C# winform中异步调用web services
如果webservice有一个Get()方法。
new出来一个Web Service得proxy以后,它会有一个GetAsync()的方法和一个GetCompleted的事件,注册GetCompleted事件,然后invoke GetAsync方法就可以了。
4.C#如何调用Word并打开一个Word文档
private void OpenMicrosoftWord(string urlorpath)
{
//启动线程
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = “WINWORD.EXE”;
startInfo.Arguments = urlorpath;
try
{
Process.Start(startInfo);
}
catch (Exception e)
{
MessageBox.Show(“Cannot start Microsoft Word, please make sure it is installed.”);
}
}
5.C#如何进行LDAP用户校验
private bool Authenticate(string userName, string password, string domain)
{
bool authentic = false;
try
{
DirectoryEntry entry = new DirectoryEntry(“LDAP://”+domain, userName, password);
object nativeObject = entry.NativeObject;
authentic = true;
}
catch (DirectoryServicesCOMException) { }
return authentic;
}
6.编写一个类体现构造,公有,私有方法,静态,私有变量
public class stu
{
private string name;
public static int count;
public stu()
{
}
public string Name
{
get{return name;}
set{name = value;}
}
private string Method1()
{
Response.Write(“私有方法”);
}
public string Method2()
{
Response.Write(“公有方法”);
}
}
7.C#语言写出在本地创建一个UDP接收端口的具体过程
:const int port = 8000 ;
//定义端口号
TcpListener tcplistener = new TcpListener(port);
Console.WriteLine(“侦听端口号: ” + port.ToString());
tcplistener.Start();
//侦听端口号
while (true)
{
Socket socket = tcplistener.AcceptSocket();
//并获取传送和接收数据的Scoket实例
Proxy proxy = new Proxy(socket);
//Proxy类实例化
Thread thread = new Thread(new ThreadStart(proxy.Run));
//创建线程
thread.Start();
//启动线程
}
8.C#语言写出与SQLSERVER访问时的具体过程
private SqlConnection conn;//定义一个数据库连接
private SqlCommand com;//定义执行命令
public data()
{
//在配置文件中设置
conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.AppSettings["connString"].ToString();
}
//打开数据库
public void open()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
//关闭数据库
public void close()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
//执行数据库操作,返回DataSet
public void getDataSet(string proc,SqlParameter[] parm,out DataSet ds)
{
com = getCommand(proc,parm);
SqlDataAdapter sda = new SqlDataAdapter(com);
ds = new DataSet();
sda.Fill(ds);
}
//返回SqlCommadn
public SqlCommand getCommand(string proc, SqlParameter[] parm)
{
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = proc;
foreach (SqlParameter p in parm)
{
com.Parameters.Add(p);
}
return com;
}
//返回DataReader
public SqlDataReader getDataReader(string proc, SqlParameter[] parm)
{
com = getCommand(proc, parm);
return com.ExecuteReader();
}
//执行数据库操作,返回void
public void ExecuteCommand(string proc, SqlParameter[] parm)
{
com = getCommand(proc, parm);
open();
com.ExecuteNonQuery();
close();
}
注意:引用 using System.Data.SqlClient,如果是连接模式记得完了后断开连接,SqlDataReader记得关闭,出错了怎么处理
9.写一个实现对一段字符串翻转的方法,附加一些条件,如其中包括“,”、“.”,对其设计测试用 例 。
答: using System.Text;
string inputStr = “a,b,c,d,e,f,g,h,i,j,k,l”;
char[] inputChar = inputStr.ToCharArray();
int count = inputChar.Length;
StringBuilder sb = new StringBuilder();
for (int m = count – 1; m >= 0; m–)
{
sb.Append(inputChar[m]);
}
10.一个长度为10000的字符串,通过随机从a-z中抽取10000个字符组成。请用c#语言编写主要程序来实现。
using System.Text;
StringBuilder sb = new StringBuilder(0, 10000);
string strABC = “a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z”;
string[] ABC = strABC.Split(‘,’);
int len = ABC.Length;
Random rd = new Random();
for (int i = 0; i < 10000; i++)
{
sb.Append(ABC[rd.Next(len)]);
}
11.对于这样的一个枚举类型:
enum Color:byte{
Red,
Green,
Blue,
Orange
}
试写一段程序显示出枚举类型中定义的所有符号名称以及它们对应的数值。
答:string[] ss=Enum.GetNames(typeof(Color));
byte[] bb=Enum.GetValues(typeof(Color));
12.用VB或VB.NET或ASP.NET编程,从一个表中选出两个varchar列(其中一列数据不能有重复,并且长度等于4),并将两列连接成一个字符串,将这些字符串动态加载到TreeView1所有节点的Text属性中
答:先从数据库中取出数据“select city,phone from authors where len(city) = 4”,如果不能重复的是city,然后在foreach循环中判断取出来的city的值,只取相同中的一个,遍历treeview
public void ShowTreeView(TreeNodeCollection Nds)
{
DataView dv = new DataView();
TreeNode tmpNd;
dv.Table=ds.Tables[0];
foreach(DataRowView drv in dv)
{
tmpNd=new TreeNode();
tmpNd.Text =“字符串的值”;
Nds.Add(tmpNd);
ShowTreeView(tmpNd.Nodes);
}
}