1. 苏葳的备忘录首页
  2. 编程

C#零碎心得

c# System.DateTime.Now.Date与静态属性System.DateTime.Today相同,都为表示当前日期的DateTime型值,并且时间部份都为夜间12:00:00即00:00:00。数据库返回的空值怎么判断?DBNull结构的Value就是这个空值,可用于比较。而datareader的isDBNULL可用来判断,对于DataSet里的DataTable里的DataRow,也有个isNull的函数用于判断某列是否为空。

控件填充窗体的方式跟Z order有关。后放上去的控件Dock属性选了Fill也只好被挤在中间。DateTime.toString()里的格式化串中,d表示一位日期,dd表示可补0的两位,而ddd表示周三的”三”,dddd则干脆返回”星期三”。

另附C#生成重复字符串的三种方式:

//用StringBuilder的,据说比简单重复相加效率高些:
static string StringRepeat(string str, int n)
{
  if (String.IsNullOrEmpty(str) || n <= 0)
    return str;
  StringBuilder sb = new StringBuilder();
  while (n > 0)
  {
    sb.Append(str);
    n--;
  }
  return sb.ToString();
}
//用递归方式的,有趣:
string Build(string value, int count)
{
 string outValue = value;
 if (count > 1)
 {
   outValue += Build(value, count - 1);
 }
 return outValue;
}
//据说内存效率最高的:
public static string RepeatString(string str, int n)
{
 char[] arr = str.ToCharArray();
 char[] arrDest = new char[arr.Length * n];
 for (int i= 0; i < n; i++)
   Buffer.BlockCopy(arr, 0, arrDest, i * arr.Length * 2, arr.Length * 2);
 return new string(arrDest);
}

 

原创文章,作者:苏葳,如需转载,请注明出处:https://www.swmemo.com/195.html

发表评论

邮箱地址不会被公开。 必填项已用*标注