c#-3.0

Logging state of object. Getting all its property values as string

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 02:42:16
问题 public class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } } ...... var emp1Address = new Address(); emp1Address.AddressLine1 = "Microsoft Corporation"; emp1Address.AddressLine2 = "One Microsoft Way"; emp1Address.City = "Redmond"; emp1Address.State = "WA"; emp1Address.Zip = "98052-6399"; Consider above class and later its initialization. Now at some

The evilness of 'var' in C#? [duplicate]

别来无恙 提交于 2019-12-20 20:28:45
问题 This question already has answers here : Closed 10 years ago . Possible Duplicate: C# 'var' keyword versus explicitly defined variables EDIT: For those who are still viewing this, I've completely changed my opinion on var. I think it was largely due to the responses to this topic that I did. I'm an avid 'var' user now, and I think its proponents comments below were absolutely correct in pretty much all cases. I think the thing I like most about var is it REALLY DOES reduce repetition

LINQ selection by type of an object

我怕爱的太早我们不能终老 提交于 2019-12-20 10:58:48
问题 I have a collection which contains two type of objects A & B. Class Base{} Class A : Base {} Class B : Base {} List<Base> collection = new List<Base>(); collection.Add(new A()); collection.Add(new B()); collection.Add(new A()); collection.Add(new A()); collection.Add(new B()); Now I want to select the objects from the collection based on its type (A or B, not both). How I can write a LINQ query for this? Please help me. Otherwise I need to loop through the collection, which I dont want to.

Can I specify a meaningful name for an anonymous class in C#?

会有一股神秘感。 提交于 2019-12-20 09:46:30
问题 We all know that when we create an anonymous class like this: var Employee = new { ID = 5, Name= "Prashant" }; ...at run time it will be of type: <>f__AnonymousType0<int,string> Is there any way to specify a meaningful name to such classes? 回答1: public class Employee {} 回答2: It's an anonymous type, that defeats the purpose. Those objects are designed to be temporary. Hell, the properties are even read-only. Sorry, I'm being a smart-ass. The answer is no, there is no way to tell the compiler

How to split string into a dictionary

房东的猫 提交于 2019-12-20 08:57:17
问题 I have this string string sx="(colorIndex=3)(font.family=Helvetica)(font.bold=1)"; and am splitting it with string [] ss=sx.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries); Instead of that, how could I split the result into a Dictionary<string,string> ? The resulting dictionary should look like: Key Value colorIndex 3 font.family Helvetica font.bold 1 回答1: There may be more efficient ways, but this should work: string sx = "(colorIndex=3)(font.family=Helvicta)(font.bold

How to add or subtract dates in C# using ajax calendar extender?

你说的曾经没有我的故事 提交于 2019-12-20 07:42:16
问题 I have two textboxes in which I have used ajax calendar extender. When I choose a date from one of the text boxes, it should automatically fill the other textbox by adding some days or months. How can I do that? 回答1: Please follow this example and modify your code. Hope it helps. <asp:UpdatePanel id="UpdatePanel1" runat="server"> <contenttemplate> <cc2:CalendarPopup id="CalendarExtender1" runat="server" Width="71px" OnDateChanged="CalendarPopup1_DateChanged" AutoPostBack="True"></cc2

How to compare two generic lists in C# 3.0? [duplicate]

早过忘川 提交于 2019-12-20 06:28:21
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Is there a built-in method to compare collections in C#? What is the best way to compare to generic two generic lists in C# 3.0? 回答1: If you want to compare regardless of element order, try the UnorderedEqual<T> method from here: C# List Element Equality Otherwise: var list1 = new List<int> { 1, 2, 3 }; var list2 = new List<int> { 1, 2, 3 }; var areSame = Enumerable.SequenceEqual(list1, list2); 回答2: Check out

how to make search of a string in a data base in c#

空扰寡人 提交于 2019-12-20 03:52:11
问题 This is the code that is used to make the search private void button1_Click(object sender, EventArgs e) { string connectionString = Tyre.Properties.Settings.Default.Database1ConnectionString; SqlConnection conn = new SqlConnection(connectionString); DataTable dt = new DataTable(); SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM table1 where Nom like " + textBox1.Text, conn); SDA.Fill(dt); dataGridView1.DataSource = dt; } and im getting this error An unhandled exception of type 'System

Using background worker with multiple classes in C#

懵懂的女人 提交于 2019-12-20 03:07:08
问题 I am learning to program in C# and have most of the basics down already. I am having trouble using the background worker and using it with multiple classes. This is a backup program that I am writing I have the following classes. lacie.cs ---> used to search for backup device main.cs ---> Main entry size.cs ---> Determines the size of the backup xml.cs ---> Reads an xml config file of directories to be backed up. I will show what I have in the main.cs so far. [main.cs code] using System;

Stop/start WCF MEX service at runtime

瘦欲@ 提交于 2019-12-20 02:52:10
问题 Is it possible/how do I stop and start the HTTP MEX listener of a self hosted WCF service at runtime without affecting the primary WCF service? (Please don't ask why I want to do this. It is a hack to get around artificial limitations imposed by someone else.) 回答1: *****[Re-added this answer after re-test and code cleanup] This is actual code that I have added to my generic WCF-based service development framework and it is fully tested.***** Assuming that you start with MEX enabled on the