Tag: C#的

像projecteuler.net网站

有时我正在projecteuler.net上解决问题。 几乎所有的问题都可以通过程序来解决,但是这些任务比编程更为math化。 也许有人知道类似的网站: devise任务, 架构任务, 像“find最优雅的C ++解决scheme”的东西?

获取程序集名称

C#的exception类具有默认情况下设置为程序集名称的源属性。 有没有另一种方法来得到这个确切的string(不parsing不同的string)? 我已经尝试了以下内容: catch(Exception e) { string str = e.Source; //"EPA" – what I want str = System.Reflection.Assembly.GetExecutingAssembly().FullName; //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" str = typeof(Program).FullName; //"EPA.Program" str = typeof(Program).Assembly.FullName; //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" str = typeof(Program).Assembly.ToString(); //"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" str = typeof(Program).AssemblyQualifiedName; //"EPA.Program, EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" }

我如何使用接口作为C#genericstypes约束?

有没有办法得到下面的函数声明? public bool Foo<T>() where T : interface; 即。 其中T是一个接口types(类似于where T : class和struct )。 目前我已经解决了: public bool Foo<T>() where T : IBase; 在哪里IBase被定义为一个空的接口,被所有我的自定义接口inheritance…不理想,但它应该工作…为什么你不能定义一个genericstypes必须是一个接口? 对于什么是值得的,我想这是因为Foo正在做reflection,它需要一个接口types…我可以作为一个普通的parameter passing它,并在函数本身进行必要的检查,但这似乎更types安全(和我想有一点更高性能,因为所有的检查都是在编译时完成的)。

如何在.NET中设置SmtpClient对象的用户名和密码?

我看到不同版本的构造函数,一个使用web.config中的信息,一个指定主机,一个指定主机和端口。 但是,如何将用户名和密码设置为与web.config不同的内容? 我们有问题,我们的内部smtp被一些高安全性的客户端阻止,我们想要使用他们的smtp服务器,有没有办法从代码而不是web.config做到这一点? 在这种情况下,如果没有数据库可用,我将如何使用web.config凭据? public static void CreateTestMessage1(string server, int port) { string to = "jane@contoso.com"; string from = "ben@contoso.com"; string subject = "Using the new SMTP client."; string body = @"Using this new feature, you can send an e-mail message from an application very easily."; MailMessage message = new MailMessage(from, to, subject, body); SmtpClient […]

如何从TabControl中隐藏TabPage

如何在WinForms 2.0中从TabControl中隐藏TabPage?

了解.NET中的垃圾收集

考虑下面的代码: public class Class1 { public static int c; ~Class1() { c++; } } public class Class2 { public static void Main() { { var c1=new Class1(); //c1=null; // If this line is not commented out, at the Console.WriteLine call, it prints 1. } GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine(Class1.c); // prints 0 Console.Read(); } } 现在,即使main方法中的variablesc1超出了作用域,并且在GC.Collect()时没有被任何其他对象进一步引用,为什么在那里没有最终确定呢?

将对象强制转换为int的更好方法

这可能是微不足道的,但我想不出一个更好的方法来做到这一点。 我有一个COM对象,返回一个变体,成为在C#中的对象。 唯一的办法,我可以得到这个int是 int test = int.Parse(string.Format("{0}", myobject)) 有没有更干净的方法来做到这一点? 谢谢

为什么auto a = 1; 在C编译?

代码: int main(void) { auto a=1; return 0; } 当文件具有.c扩展名时,由MS Visual Studio 2012编译器编译时没有错误。 我一直认为,当你使用.c扩展名时,编译应该按照C语法,而不是C ++。 此外,据我所知,没有types的auto 只能在C ++ 11以后的C ++中使用,这意味着types是从初始化器中推导出来的。 这是否意味着我的编译器不是坚持C,还是C语言中的代码实际上是正确的?

删除尾随零

我有一些由一个集合返回的字段 2.4200 2.0044 2.0000 我想要的结果像 2.42 2.0044 2 我试着用String.Format ,但它返回2.0000并将其设置为N0四舍五入其他值以及。

什么是检查空值的正确方法?

我喜欢null-coalescing运算符,因为它可以很容易地为可为null的types分配默认值。 int y = x ?? -1; 这很好,除非我需要用x做简单的事情。 例如,如果我想检查Session ,那么通常最终必须写更详细的内容。 我希望我能做到这一点: string y = Session["key"].ToString() ?? "none"; 但是你不能这样做,因为.ToString()在空检查之前被调用,所以如果Session["key"]为空,它就会失败。 我最终这样做: string y = Session["key"] == null ? "none" : Session["key"].ToString(); 在我看来,它比三线式更好, string y = "none"; if (Session["key"] != null) y = Session["key"].ToString(); 即使这样做,我仍然好奇,如果有更好的方法。 似乎不pipe我总是必须参考Session["key"]两次; 一次为检查,再次为任务。 有任何想法吗?