The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [c#]

C# is a multi-paradigm, managed, garbage-collected, object-oriented programming language created by Microsoft in conjunction with the .NET platform. Use this tag for questions related to C#. In case a specific version of the framework is used, you could also include that tag; for instance .net-2.0.

Filter by
Sorted by
Tagged with
3
votes
2answers
1k views

Loading either all shops or a specifically named shop from a DataTable

...
3
votes
1answer
9k views

Determining if current thread is executing

Using C#, create a class which gets an action (parameter-less delegate) in its construction and has a single public method bool execute() which does the ...
2
votes
1answer
4k views

Encoding on Dropdownlist items

I am currently doing Encoding on Dropdownlist in our project. Could you please review and provide your comments about the following approach? After the encoding on dropdown, if there are any special ...
14
votes
6answers
3k views

Showing various network information from a NIC card

I have included the code from a second year project that I would like some advice on. Basically it utilises WMI to query a NIC card from a selection (depending how many are installed in the host ...
4
votes
3answers
9k views

There has to be a better way of parsing XML in C# than what I'm doing

This is the way I'm currently getting data from XML, but it seems to be really inefficient, checking every localname in every iteration. How should I be doing it? Here is a sample of the XML I am ...
1
vote
3answers
747 views

Is my multithreaded form code correct?

The basic idea is this: There are two buttons: a DoWork button and a Cancel button. The ...
0
votes
1answer
2k views

How to re register dynamic scripts registered with ClientScriptManager in asp dot net using session was bad idea

Following the normal pattern of adding inline scripts to gridview row/s is a bad practice it used to work like below ...
3
votes
2answers
985 views

Fair use or Abuse of the dynamic (System.Dynamic.DynamicObject)?

I have a T4 Templates project that generates methods to "convert" one Type (class, struct, enum) from a source Assembly its corresponding Type in another. The two types have the identical names and ...
4
votes
4answers
487 views

Email confirmation model

Email<input type="text" id="email" /><br /> Re-enter Email<input type="text" id="confirm-email" /> I'm thinking that I want a model so that I ...
1
vote
1answer
591 views

Adding or Replacing a Page item in a QueryString in C#.

Can this code be improved? I'm avoiding using the QueryString collection because the query string may contain eg: OrderBy=Column1&OrderBy=Column2 and it didn't ...
2
votes
1answer
905 views

WCF general response validation for multiple services

Background information I am building a web application that consumes 3 different WCF services that are hosted internally. Each response object from the service inherits from a base class ...
4
votes
1answer
314 views

Using Regex to parse a chat transcript

I need to classify each line as "announce, whisper or chat" once I have that sorted out I need to extract certain values to be processed. Right now my regex is as follow: ...
8
votes
4answers
3k views

“Pretty” date generator

I have this "pretty" date string generator in C# - pass it a date and it returns a string with "5 minutes ago" or "2 weeks, 3 days ago", etc. It's a little verbose and hogs 61 lines, and I'm ...
33
votes
6answers
4k views

DisposableObject base class for C#

I commonly run into the need to implement IDisposable in my code. To correctly dispose of both managed and unmanaged resources requires a reasonable amount of boilerplate code. Based on the ...
7
votes
3answers
4k views

TDD workflow best-practices for .NET using NUnit

I'm starting to dive into TDD with NUnit and despite I've enjoyed checking some resources I've found here at stackoverflow, I often find myself not gaining good traction. So what I'm really trying to ...
5
votes
3answers
391 views

Improving the design of this program.

I am trying to design a program that will read a XML file and based on the tag name will perform certain requirements checks. I feel that there is probably a better way of doing this and any comments ...
11
votes
3answers
6k views

Waiting for a lock to release with Thread.Sleep()?

There's a follow-up: Waiting for a lock to release with ManualResetEvent and Quartz. I've written a simple Lock-Mechanism which is saving the states of the locks in a database. Now I need to wait for ...
6
votes
2answers
7k views

Unit (Integration) Test for code that interacts with a database

I am trying to create some (I believe these are Integration not Unit) tests for code that interacts with a database and I am not sure this is the right approach. I would appreciate any reviews and ...
19
votes
1answer
19k views

LINQ to SQL Joining Entities

I have two tables, one for jobs, and one for the names of industries (e.g. automotive, IT, etc). In SQL I would just do: ...
9
votes
1answer
3k views

Database Class Creator

Overview I've written a class that will create C# code. The output is a single .cs file for each table in the default database (the database is defined inside a web.config file). I'm looking for a ...
5
votes
6answers
9k views

Data Access Layer Code

Wondering if I could do this a little bit better. Currently, my DAL has a blank constructor that sets the connection string from the web.config. ...
6
votes
2answers
9k views

Database access class

I'm looking for any comments or feedback on my database access class. Security and speed are two things I'm most concerned about. One thing to note is this class has to work in a C# .NET 2 ...
4
votes
2answers
767 views

Streamline code for checking if a directory exists and saving a file

I hate this code. What is the slickest way to write the following: ...
2
votes
1answer
226 views

Is Refactoring with different class casting and property calling possible ?

Can i refactor these code ? The diffrent between these block of code are. They are using different casting type. They call different property ...
15
votes
2answers
4k views

Constructing a Data Contract interface that can be used with Entity Framework 4.1 to ensure entity validity

I need some thoughts about an approach a colleague and I am are taking with Entity Framework. Basically, the entities are represented by contracts. These contracts hold a collection of business rules ...
6
votes
2answers
13k views

Asynchronous Sockets

I need to implement a TCP client application. The client and the server send messages to each other. I want to make this program scalable enough to handle connections to multiple servers at the same ...
9
votes
4answers
2k views

Is catching 'expected' exceptions that bad?

I had to find the corresponding process for a windows service the other day, and I kept on getting this one exception. I've spent some time researching the exception only to find that this exception ...
4
votes
4answers
478 views

Comment formatter

...
14
votes
2answers
1k views

“Pluggable” routes in ASP.NET MVC

How do I handle multi-tenancy with routes in ASP.NET MVC? The application is a multitenant ASP.NET MVC 3 application. Tenants are identified based on hostname so this does not interfere with routing. ...
4
votes
1answer
747 views

IMapDocument in a 10.1 SOE

I am wondering if this code will work in 10.1. This video at 1:02 says not to create mxd based services. I think that just means not using IMapServerObjects3, but seems like IMapDocument should be OK....
9
votes
2answers
3k views

Async Task implementation

Beeing on .NET 3.5 i don't have access to the TPL. Yet i have become fed up of having to manage manually the logic behind delegate.BeginInvoke type of scenarios each time and i set up to implement my ...
12
votes
5answers
1k views

To make a calculator from scratch

As a personal challenge, I am trying to make a very basic calculator without using any CLR's integer and arithmetic operations, but to only use memory. The idea was to do what CLR/OS does. i came up ...
4
votes
2answers
1k views

TextWriterTraceListener class

I need to extend the default TextWriterTraceListener class to include timestamps with each message on same line and file rotation. I can't seem to find any way of ...
7
votes
2answers
2k views

Slow-running File parser

I've got a method that parses a file. I take all the words and add them to a SortedSet. Every word contains a list of Lines ...
5
votes
4answers
3k views

Is this architecture overly complex or following best practices?

I work as a C# developer at a company that doesn't use best practices at all. We're on .NET 3.5 but most code is written in a .NET 1.1 style (e.g. almost all the logic is in the code behind of the ...
3
votes
2answers
467 views

Open database and hande errors

How to make this code better? ...
5
votes
1answer
1k views

Return Dictionary object having key/value pair of keywords and URLs

Can you please help me split this method to reuse the repeating code? Any advice/comments on the code are welcome. ...
5
votes
1answer
5k views

Optimizing the reading of web.config

Here's the issue. When I build an asp.net application I create a singleton to read the values from the web.config file so that the values are only read once and gives a slight speed increase to the ...
15
votes
1answer
7k views

What is better for Lazy-Loading Navigation Properties of detached Self-Tracking Entities through a WCF service?

I have a WCF client which passes Self-Tracking Entities to a WPF application built with MVVM. The application itself has a dynamic interface. Users can select which objects they want visible in their ...
4
votes
2answers
5k views

Is this a reasonable Trie implementation?

http://en.wikipedia.org/wiki/Trie The basic idea is to support fast searches via prefix matching. In my implementation, I allow each node to store all of the words that match its given prefix, and ...
4
votes
1answer
232 views

Wrapper for dynamic behavior

I am writing a wrapper for dynamic behavior in C#. If I use GetMethod(), then an exception is thrown when there is more than one method (overloads). I figure it ...
14
votes
10answers
12k views

Looping through a collection

In the situation where I am looping through a collection looking for an item, is it better practice to break before I return or ...
7
votes
4answers
659 views

Managing part types and counts

Both functions look fairly similar to each other, but they are different in the type of string. I have 6 functions like this and all differ only in the "string cmd". Any ideas on how I can put it all ...
6
votes
3answers
1k views

Performance optimization for Linq To SQL

I have been working with C# for quite some time but relatively new to the concepts of lambdas and Linq. I was working with a Linq To SQL example and trying to write a generic solution for executing a ...
10
votes
3answers
1k views

FTP Download Helper Method

I'm currently using an Open Source FTP library in all my projects as I've never managed to code a decent FTP Helper Library based on the C# 4.0 API. What do you think about this approach? I'm not ...
7
votes
2answers
32k views

Is there a better way to search directories for a file or files?

Edit: The purpose of this method is to search a specific directory for a given file type, ex. (*.txt, *.pdb, *.exe") then move the file(s) to a given destination directory and delete the original ...
6
votes
1answer
4k views

Why is this code to paste into a DataGridView so slow?

...
31
votes
1answer
16k views

Extension methods to make ConcurrentDictionary GetOrAdd and AddOrUpdate thread safe when using valueFactory delegates

The ConcurrentDictionary<T,V> in .NET 4.0 is thread safe but not all methods are atomic. This points out that: ... not all methods are atomic, ...
3
votes
1answer
666 views

MVP presenter code in C# - possible troubles with DTO's

I am making a website using the MVP pattern. It is a simple website, so for the repository I use Linq2SQL. My architecture is basically: (View - Presenter) - Service Layer - Domain Models. The View ...
6
votes
1answer
1k views

View Model Base

I needed to write code in WPF for a client. The application is done with prism (not my decision, they already had people working on it), so I got a sparkling clean new module to work in. Since this ...