c#-2.0

static readonly field initializer vs static constructor initialization

时光怂恿深爱的人放手 提交于 2019-11-27 18:21:11
Below are two different ways to initialize static readonly fields. Is there a difference between the two approaches? If yes, when should one be preferred over the other? class A { private static readonly string connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } class B { private static readonly string connectionString; static B() { connectionString = WebConfigurationManager.ConnectionStrings["SomeConnection"].ConnectionString; } } Mark Byers There is one subtle difference between these two, which can be seen in the IL code - putting an explicit

DateTime string parsing

别说谁变了你拦得住时间么 提交于 2019-11-27 18:03:49
问题 I have made a generic parser for parsing ascii files. When I want to parse dates, I use ParseExact function in DateTime object to parse, but I get problems with the year. The text to parse is i.e. "090812" with the parseExact string "yyMMdd". I'm hoping to get a DateTime object saying "12/8-2009", but I get "12/8-1909". I know, that I could make an ugly solution by parsing it afterwards, and thereby modifying the year.. Anyone know of a smart way to solve this ?? Thanks in advance.. Søren 回答1

Using Reflection to set a static variable value before object's initialization?

别来无恙 提交于 2019-11-27 17:14:36
问题 Is there anyway to set the value of a static (private) variable on an object that has not been initialized? The SetValue method requires an instance, but I'm hoping there's a way to get around this. 回答1: For static values you can pass null for the instance parameter. var type = typeof(SomeClass); var field = type.GetField("SomeField", BindingFlags.NonPublic | BindingFlags.Static); field.SetValue(null, 42); 回答2: could you create a static function that is public and use it to set your private

Best practice: How to expose a read-only ICollection

依然范特西╮ 提交于 2019-11-27 16:32:42
问题 I have an ICollection<T> called foos in my class which I want to expose as read-only (see this question). I see that the interface defines a property .IsReadOnly , which seems appropriate... My question is this: how do I make it obvious to the consumer of the class that foos is read-only? I don't want to rely on them remembering to query .IsReadOnly before trying a not-implemented method such as .Add() . Ideally, I would like to expose foos as a ReadOnlyCollection<T> , but it does not

C# Xml Serialization & Deserialization

浪子不回头ぞ 提交于 2019-11-27 14:25:47
I am trying to serialize an object & save it into a Sql server 2008 xml field. I also have some deserialization code that re-hydrates the object. I am able to serialize & save the object into the db, but get a "Root element missing" exception. [XmlRoot("Patient")] public class PatientXml { private AddressXml _address = null; private EmergencyContactXml _emergencyContact = null; private PersonalXml _personal = null; [XmlElement] public PersonalXml Personal { get { return _personal; } set { _personal = value; } } [XmlElement] public AddressXml Address { get { return _address; } set { _address =

C# ASP.NET Send Email via TLS

喜欢而已 提交于 2019-11-27 13:50:47
In order to comply with HIPAA regulations, we need to send email from an external site (outside the firewall) to an internal Exchange server (inside the firewall). Our Exchange admins tell us we need to use TLS encryption to send mail from the web server to the email server. I've never used TLS before and I'm not very familiar with it. Searching on Google as brought up numerous paid-for-use libraries. Is there anything native to .NET that will accomplish this? If so, how do I configure it? If not, is there something free or open source? Current Configuration: ASP.NET C# Web Application 2.0

Speed up File.Exists for non existing network shares

假如想象 提交于 2019-11-27 13:41:17
问题 I have to check if a set of file paths represent an existing file. It works fine except when the path contains a network share on a machine that's not on the current network. In this case it takes a pretty long time (30 or 60 seconds) to timeout. Questions Is there a way to shorten the timeout for non existing network shares? (I'm certain that when they do exist they'll answer quickly, so a timeout of 1 sec would be fine) Is there any other way to solve this issue without starting to cache

ExecuteNonQuery requires the command to have a transaction error in my code

不问归期 提交于 2019-11-27 12:41:24
问题 I get the following error on cmd.ExecuteNonQuery . "ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized." Here is my code: //if (hdRefresh.Value.Length > done.Value.Length || done.Value == "1") //{ // //Write Your Add Customer Code here > Response.Write("true") // done.Value = hdRefresh.Value; //} //else //{ // Response.Redirect("~/Cashier

List of new features in C# 2.0, 3.0 and 4.0 [closed]

空扰寡人 提交于 2019-11-27 12:29:26
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I worked on the .NET 1.1 project for a long time, and I was stuck at C# 1.0 and now I would like to catch up with the latest and

C# thread pool limiting threads

浪尽此生 提交于 2019-11-27 11:20:30
Alright...I've given the site a fair search and have read over many posts about this topic. I found this question: Code for a simple thread pool in C# especially helpful. However, as it always seems, what I need varies slightly. I have looked over the MSDN example and adapted it to my needs somewhat. The example I refer to is here: http://msdn.microsoft.com/en-us/library/3dasc8as(VS.80,printer).aspx My issue is this. I have a fairly simple set of code that loads a web page via the HttpWebRequest and WebResponse classes and reads the results via a Stream . I fire off this method in a thread as