1.在面向对象中既然结构体中可以有成员变量也可以有方法等,那为什么还要有类的存在呢?
:结构体是值类型,而类是引用类型。然后就是值类型和引用类型之间的区别了。
2.String 和string有何区别?
: 通过typeof()的测试可以发现两者的结果都是System.String类型。其实string是String的别名。它们唯一的区别是按照代码约定来编写,还是用类的方式来编写。
3.如何在c#控制台应用程序中获取应用程序的路径?
例如:string path=System.Reflection.Assembly.GetExecutingAssembly().Location();
Console.WriteLine(System.IO.Path.GetDirectoryName(path));
4.如何快速的在一个字符串中统计另一个字符串出现的次数。
:可以利用System.Text.RegularExpressions名称空间中的Regex类
例如:
String test=”hello ,how are you you ,you you youyou”;
Int wordcount=0;
Foreach(Math m in Regex.Mathches(test,”you”))
{
Wordcount++;
}
5.如何快速的检测一个数是否是2的幂
举一个简单的例子:
大家都知道8是2的幂,而8的二进制为 1 0 0 0,7的二进制为 0 1 1 1
那么 1 0 0 0
& 0 1 1 1
0 0 0 0
大家也知道9不是2的幂,9的二进制为 1 0 0 1, 8的二进制为 1 0 0 0
那么 1 0 0 1
& 1 0 0 0
1 1 1 0
由此可得以下算法:
If(number!=0&&(number&(number-1))==0)则number是2的幂
6..NET中如何实现字符串的加密解密
核心代码如下:
#region String Encryption
string plainText="this is plain that we will encrypt";
string password="p@$$w0rd";
Console.WriteLine(plainText);
Console.Writeln();
DESEncrypt testEncrypt=new DESEncrypt();
string encText=testEncrypt.EncryptString(plainText,password);
Console.WriteLine(encText);
Console.WriteLine();
string plain=testEncrypt.DecryptString(encText,password);
Console.WriteLine(plain);
Console.WriteLine();
#endregion
class DESEncrypt
{
static TripleDES(string key)//对密码进行散列编码
{
MD5 md5=new MD5SCryptoServiceProvider();
TripleDES des =new TripleDESCryptoServiceProvider();
des.key=md5.ComputeHash(Encoding.Unicode.GetBytes(key));
des.IV=new byte[des.BlockSize/8];
return des;
}
public string EncryptString(string plainText,string password)//加密过程
{
byte[] plainTextBytes=Encoding.Unicode.GetBytes(plainText);
MemoryStream myStream=new MemoryStream();
TripleDES des=CreateDES(password);
CryptoStream cryptStream=new CryptoStream(myStream,des.CreateEncryptor(),CryptoStreamMode.Write);
cryptStream.Write(plainTextBytes,0,plainTextBytes.Length);
cryptStream.FlushFinalBlock();
return Convert.ToBase64String(myStream.ToArray());
}
public string DecryptString(string encryptedText,string password)//解密过程
{
byte[] encryptedTextBytes=Convert.FromBase64String(encryptedText);
MemoryStream myStream=new MemoryStream();
TripleDes des=CreateDES(password);
CryptoStream decryptStream =new CryptoStream(myStream,des.CreateDecryptor(),CryptoStreamMode.Write);
decryptStream.Write(encryptedTextBytes,0,encryptedTextBytes.Length);
decryptStream.FlushFinalBlock();
return Encoding.Unicode.GetString(myStream.ToArray());
}
}