【C#常用公共方法】教程文章相关的互联网学习教程文章

C# WinForm下,隐藏主窗体的方法【代码】

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace GetHTMLContent {publicpartialclass Form1 : Form{public Form1(){InitializeComponent();this.WindowState = FormWindowState.Minimized;this.ShowInTaskbar = false;SetVisibleCore(false); }protectedoverridevoid Set...

C# Windows DataGridView 判断CheckBox 选取的方法【图】

单行选中foreach (DataGridViewRow dr in this.dataGridView1.Rows) { try { //DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dr.Cells[0]; //if ((bool)cbx.FormattedValue) if(dr.Cells[0].Selected) { arrShiftCode.Add(dr.Cells[1].Value); ...

C#接口使用方法【代码】

using System;namespace ClassLibrary2 { interface IEmploy //接口 { void Speak(); //方法 }class Hello:IEmploy //Hello类实现接口 {publicvoid Speak() //实现方法 { Console.WriteLine("Hello:朋友"); }}class Sorry:IEmploy //Sorry类实现接口 {publicvoid Speak() //实现方法 { Console.WriteLine("Sorry:朋友"); }} } //实现//直接调用 IEmploy Ie = new Hello(); Ie.Speak(); //调用Hello类实现的接口 IEmpl...

C#的TextBox的四种禁止编辑方法【代码】

前言一般而言,Textbox中有两个属性可以对其进行防止编辑的设定,这是最基础的知识,也是我要提出的前两种方法。而后两种方法实际为一种,但可以应用于不同环境中。一、ReadOnly属性这样设置,Textbox控件则限制不能输入,但可以读取已有文本,样式也与正常使用的Textbox一致。1、在前端设置Readonly为true,样式如下: <asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" Text="测试"></asp:TextBox> 2、在后台设置Reado...

C# List 根据对象属性去重的四种方法对比【代码】

private void TestDistinct() {Task.Run(() =>{//生成测试数据DateTime dt = DateTime.Now;Random rnd = new Random();List<MyData> list = new List<MyData>();int total = 1000000;for (int i = 0; i < total; i++){MyData info = new MyData();info.id = rnd.Next(1, total * 10).ToString();info.name = rnd.Next(1, total * 10).ToString();list.Add(info);}double d = DateTime.Now.Subtract(dt).TotalMilliseconds;//方法一...

C# 获取磁盘空间大小的方法【代码】

方法一:利用System.IO.DriveInfo.GetDrives方法来获取/// /// 获取指定驱动器的空间总大小(单位为B) /// /// 只需输入代表驱动器的字母即可 (大写) /// public static long GetHardDiskSpace(string str_HardDiskName) { long totalSize= new long(); str_HardDiskName=str_HardDiskName +":\\"; System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo drive in drives) { if...

C# .NET4.0 改为 到.NET2.0 时 TypedTableBase 报错解决方法

.NET 4.0 降版本 到.NET 2.0。不出意外,问题必然来了。编译错误一:错误 1 命名空间“System”中不存在类型或命名空间名称“Linq”(是缺少程序集引用吗?)解决:删掉该引用--没用到情况下,呵呵使用Linq时,引用一个LinqBridge.dll 就可以了编译错误二:错误 2 命名空间“System.Data”中不存在类型或命名空间名称“TypedTableBase”(是缺少程序集引用吗?)解决:该错误出现在代码生成工具生成Dataset的片段里面。.NET 4.0 : public...

C# String的扩展方法ReplaceFirst【代码】

/// <summary>/// 替换第一个符合条件的字符串/// </summary>/// <param name="value"></param>/// <param name="oldValue">所要替换掉的值</param>/// <param name="newValue">所要替换的值</param>/// <returns>返回替换后的值 所要替换掉的值为空或Null,返回原值</returns>public static string ReplaceFirst(this string value, string oldValue, string newValue){if (string.IsNullOrEmpty(oldValue))return value;int idx ...

C#调用默认浏览器打开网页的方法

1.最常用的页面跳转(原窗口被替代):Response.Redirect("XXX.aspx");2.利用url地址打开本地网页或互联网:Respose.Write("<script language=‘javascript‘>window.open(‘"+ url+"‘);</script>");3.原窗口保留再新打开另一个页面(浏览器可能阻止,需要解除):Response.Write("<script>window.open(‘XXX.aspx‘,‘_blank‘)</script>");4.效果同1中的另一种写法:Response.Write("<script>window.location=‘XXX.aspx‘</scr...

c#获取ip的方法cdn加速获取真实ip方法

服务端://方法一HttpContext.Current.Request.UserHostAddress; //方法二HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];//方法三string strHostName = System.Net.Dns.GetHostName();string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue (0).ToString();//方法四(无视代理,使用cdn加速可以用这个)HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];客户端://...

C#中TransactionScope的使用方法和原理【代码】

在.net 1.1的时代,还没有TransactionScope类,因此很多关于事务的处理,都交给了SqlTransaction和SqlConnection,每个Transaction是基于每个Connection的。这种设计对于跨越多个程序集或者多个方法的事务行为来说,不是非常好,需要把事务和数据库连接作为参数传入。在.net 2.0后,TransactionScope类的出现,大大的简化了事务的设计。示例代码如下:static void Main(string[] args){using (TransactionScope ts = new Transacti...

C#跳出循环的几种方法的区别【代码】【图】

break是循环结束执行,执行循环体后面的代码。 continue是跳过本次循环未执行的代码,继续执行下一次循环。 goto是跳到指定的指令去,你指哪,他跳到哪。 return是函数返回,如果循环在Main函数中,那么程序一般就结束了。原文:http://www.cnblogs.com/iack/p/3516690.html

c# 7.1 Async Main方法【代码】

安装 .net framework sdk 7.1新建一个 .net framework 7.1 的程序在程序的工程文件的第一个 PropertyGroup 节点下加入以下子属性 <LangVersion>7.1</LangVersion>编码staticasync Task Main(){await Test();}static Task Test(){Console.WriteLine("called async method");return Task.CompletedTask;} .net core 2.0也支持,直接看第三步原文:https://www.cnblogs.com/myesn/p/csharp71-async-main.html

C#变量初始化问题:字段初始值无法引用非静态字段、方法或属性【代码】

一上代码,后解释using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace TestStatic {class Program{staticvoid Main(string[] args){Console.WriteLine("解决字段初始化无法引用非静态字段问题");A a = new A(new B());Console.WriteLine(a.restrs); //利用属性调用就可以解决 }}publicclass A{public A(B obj){this.str = obj;Console.WriteLine(str.Say2()); //利用构造...

C# DataTable的詳細使用方法

在项目中经经常使用到DataTable,假设DataTable使用得当,不仅能使程序简洁有用,并且可以提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结。 一、DataTable简单介绍 (1)构造函数 DataTable() 不带參数初始化DataTable 类的新实例。 DataTable(string tableName) 用指定的表名初始化DataTable 类的新实例。 DataTable(string tableName, string table...