首页 > 技术文章 > 知识点总结

YYkun 2017-09-25 15:30 原文

1.ref和out的区别?
使用ref关键字时,调用方法前必须初始化参数的值,被调用的方法可以对该值进行读取或写入;
使用out关键字时,在调用方法之前可以不进行初始化,必须在方法中进行初始化。

2.Var关键字的使用注意事项?

a、var 仅能声明方法内的局部变量
b、var 声明的变量在被赋值后类型即确定下了,后续程序中不能在赋其他类型的值
c、var x = new object() 没有意义,不要写这样的代码...............

3.WPF DataGrid添加编号列?

第一步:
<DataGridTemplateColumn Header="编号" Width="50" MinWidth="10" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridRow}}, Path=Header}"
HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,0,0,0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
第二步:添加DateGrid LoadingRow 事件,如下所示:
private void dgvList_LoadingRow(object sender, DataGridRowEventArgs e)
{
  e.Row.Header = e.Row.GetIndex() + 1;
}

 4.计时器

DispatcherTimer StopDialogWindow = new System.Windows.Threading.DispatcherTimer();
// 当间隔时间过去时发生的事件
StopDialogWindow.Tick += new EventHandler(ColseDialogWinodw);//ColseDialogWinodw为需要调用的方法
StopDialogWindow.Interval = new TimeSpan(0, 0, 0, 10);
StopDialogWindow.Start();

5.计算指定时间之间相差小时数

string str = "2018-02-06 11:19:05";
DateTime date = DateTime.Parse(str);
DateTime now = DateTime.Now;
double aa = (now - date).TotalHours;
if (aa>=24)
{
MessageBox.Show("超过24小时");
}

6.string[] text = logstr.Split(new string[] { "\\n" }, StringSplitOptions.None);

 7.根据数字返回:001,002...

/// <summary>
/// 返回1--001,2--002
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public string GetValueByNum(int num)
{
return num.ToString().PadLeft(3, '0');//001
}

推荐阅读