C# 学习笔记
2015-08-11 16:07
78 查看
参数传递
基本类型默认传值,类默认穿引用。 使用ref与
out指定传引用;其中
out参数需事先进行初始化。
void func1(ref int n){ n = 1; } void func2(out int n){ n = 2; } int n = -1, m; //n 赋值为 1 func1(ref n); //异常 func1(out m); //n 赋值为 2 func2(ref n); //m 赋值为 2 func1(out m);
类型转换
//转换不成功会产生异常 int int.Parse(string s) //返回是否转换成功,若转换失败 i 将赋值为0 bool int.TtyParse(string s, out int i);
应用程序路径
1.获取模块的完整路径。 csharp System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
2.获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
csharp System.Environment.CurrentDirectory
3.获取应用程序的当前工作目录。
csharp System.IO.Directory.GetCurrentDirectory()4.获取程序的基目录。
csharp System.AppDomain.CurrentDomain.BaseDirectory5.获取和设置包括该应用程序的目录的名称。
csharp System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase6.获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已
csharp System.Windows.Forms.Application.StartupPath
7.获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。
csharp System.Windows.Forms.Application.ExecutablePath
随机数
产生随机数Random rand=new Random();//每次产生的序列是一样的。
应用程序设置
添加属性右击项目->属性->设置,添加相应类型的设置数据项。
操作属性
using namespace_of_app.Properties; Settings.Default.var1 = 333; Settings.Default.save();
文件读写
//读文件 StreamReader sr = new StreamReader ("i.txt"); sr.ReadLine(); sr.Close(); //写文件 StreamWriter sw = new StreamWriter("i.txt"); sw.WriteLine(""); sw.Close();
文件系统
访问文件系统using System.IO DirectoryInfo dir = new DirectoryInfo(szPath); FileInfo[] fs = dir.getFiles(); DirectoryInfo[] ds = dir.getDirectories();
文件系统对话框
//浏览文件 OpenFileDialog openDialog = new OpenFileDialog(); if(DialogResult.OK == openDialog.ShowDialog) { string filename = openDialog.FileName; } //浏览目录 using System.Windows.Forms; FolderBrowserDialog openFolder = new FolderBrowserDialog(); if (DialogResult.OK == openFolder.ShowDialog()) { string dirname = openFolder.SelectedPath; }
WPF ListView 数据绑定
在设置UI控件的DataContex之后,它和它的子元素都可以访问该DataContex的属性(public,并且置有读的访问器)。绑定:将itemsource置为Dictionary的Values
自动更新:实现INotifyPropertyChanged接口,更简便的方法是用ObservableCollection(它默认实现了该接口)
WPF 技巧
获取ContexMenuItem的触发控件ContextMenuService.GetPlacementTarget((sender as MenuItem).Parent) as StackPanel
设置父控件
Window.Owner = AnotherWindow
获得子窗口
Window.OwnedWindows
除非注明,本博客文章均为原创,转载请以链接形式标明本文地址: http://harttle.com/2015/05/14/csharp-note.html
相关文章推荐
- C#中的枚举类型
- C#操作目录和文件讲解
- C#Winform常引用的库
- C#中StreamReader读取中文时出现乱码问题总结
- C# 大文本的读取处理
- C#定点任务代码 类似Windows计划任务(健壮性高)
- 学习c#中遇到的一些问题(2)
- C#实现将图片另存到新建的文件夹中
- C# 基础小知识之yield 关键字 语法糖
- C#程序中将图片转换为byte数组,并将byte数组转换为图片
- C# Random 生成不重复随机数
- c# workwith .ini file
- 观察者模式(observer行为)c#简单的例子
- 使用PostSharp在.NET平台上实现AOP (c# AOP)
- C# 求时间差
- 引用一个自定义类库中不包含相应的实体类
- C#中静态函数的作用
- C# 知识回顾 - 扩展方法解析
- C#开发COM组件
- C#语法笔记