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
1
vote
2answers
12k views

Reading file contents into a string

Option 1 - nice and simple ...
11
votes
2answers
23k views

Mapping ExpandoObject to another object type

I am working on a helper method that maps properties from an ExpandoObject to a user supplied object and was wondering if the code could be cleaned up or made any ...
7
votes
1answer
12k views

Pulling a subset of elements from a Dictionary

I have a function that returns a set of values from a Dictionary. I don't currently want to allow it to throw ...
5
votes
3answers
659 views

Not feeling 100% about my Controller design.

Basically, I'm uploading an excel file and parsing the information then displaying what was parsed in a view. ...
4
votes
2answers
2k views

Is this Recursion + Linq example inefficient?

I have a hierarchy of of groups, and I want to get a collection of all the lowest level groups (or leaves if we'll look at this as a tree). I wrote the following code. Is it inefficient? ...
7
votes
1answer
429 views

Implementation of costreams

I am not very good with thread-safety and often fall prey to subtle issues in concurrency. Therefore, I hope that someone here might be able to tell me whether there is a subtle concurrency issue (...
8
votes
7answers
1k views

Refactoring a bunch of and statements

Trying to refactor this code and can't seem to think of a way to make it cleaner. Type is a property in my Person class. ...
11
votes
5answers
7k views

Deep nesting when looping over an object model coming from a 3rd part

I am stuck with this horrible object model coming back from a 3rd party product. It's six levels of objects deep, and I have to loop through the collection in each level, to get the values I need, in ...
6
votes
3answers
797 views

IEnumerable of classes that implement a given interface at runtime

I am implementing the command pattern in a project that I am working on and I have an interface, ICommandFactory that all of my commands are implementing. When I ...
8
votes
2answers
10k views

Converting objects to type Bool

The following extension method is being used in our code base: ...
8
votes
3answers
849 views

Capitalize words in sentence

...
11
votes
4answers
755 views

Interrupt lengthy for loops

I have this piece of code I would like some help with. I'm running a lengthy process that I want to interrupt at any time. The thing is, as you can see in the code, I have ...
10
votes
2answers
2k views

Am I overlooking anything with this asynchronous read/write loop?

Here's my code: ...
9
votes
3answers
2k views

Is there a better way to output a javascript array from ASP.net?

I often run into the problem of producing a javascript array on an ASP.net page from an IEnumerable and I was wondering if there was an easier or clearer way to do it than ...
228
votes
14answers
38k views

Calculating entropy of a string

We're calculating entropy of a string a few places in Stack Overflow as a signifier of low quality. I whipped up this simple method which counts unique characters in a string, but it is quite ...
4
votes
3answers
2k views

Is ToIEnumerable<T> good practice?

I have an extension method that does this: ...
38
votes
6answers
7k views

Calculation of prime numbers making use of Parallel.ForEach

In my spare time I decided to write a program that would systematically identify prime numbers from 2 to 18,446,744,073,709,551,615. This is for fun and learning, as I know it will take too long to ...
3
votes
2answers
509 views

Composable Locks using LINQ. Can anyone see any problem with this ?

I was playing around with LINQ and I came up with the following idea for composing locks taking advantage of C# Monadic syntax. It seems too simple, so I thought let me post it on StackExchange and ...
5
votes
6answers
1k views

Strongly-typed reading values from CSV DataTable

Is there a way to do this using parameters so the value is automatically converted to whatever datatype the keyfield has in the datatable? This code should be reusable for future bulk update ...
9
votes
7answers
60k views

How to break up long lines of code. (Example Line: Results of method call added to list.)

Often I generate long lines of code such as the following... ...
5
votes
2answers
667 views

Better ways to build a linq specification for querying or is this the only way?

The code pretty much explains what I am doing here. Just wondering if anyone can think of a better way. ...
3
votes
1answer
328 views

Inserting and updating time spans in XML

I need help to refactor my code. I usually had a hard time figuring out how to make my code reusable. I have an XML file that hold the data for each Tag element. Tag element should have child nodes ...
8
votes
4answers
1k views

Applying filters to a table

I've written a lengthy procedure that I call a few times to apply filters defined by the customer to a table, as I didn't see how I could turn my column name string into a LINQ column and how I could ...
8
votes
3answers
7k views

Implementation of GroupBy<TKey, TElement> in .NET

I've implemented the GroupBy extension method for IEnumerable<T> type as an excersise to deep a little more into LINQ. ...
7
votes
2answers
4k views

Automatic translation of forms

I want my application to support multiple UI-languages (aka i18n). To do so, I have built the static class below, to automatically translate the form and all its contents to the desired language. It ...
9
votes
4answers
7k views

Review of object oriented design for a sample interview question

I'm going over the interview questions from "Cracking the Coding Interview" and one of the chapters (Chapter 7) deals with Object Oriented Design (OOD). The requirements for one of the problems are as ...
5
votes
1answer
292 views

Interop: Return Parameter Method as Void

For interop purpose, this is something that I always do (C#): public static extern BigObject InteropWithCPlusPlus(); where ...
7
votes
4answers
6k views

Basic folder tree with a list of strings

I am building up a basic folder tree with a list of strings in the form /root/node/node/node. Here is the basic algorithm I am using currently to build this collection and fill in my ...
4
votes
3answers
591 views

Find the center of n squares which together build a rectangle

I have a rectangle with size w and height h. Now I want to split this rectangle into n new ...
4
votes
2answers
2k views

Multiple explicit cast operations

This sample code works fine, but it looks awful. How would you improve this? ...
12
votes
4answers
2k views

LL(1) tokenizer for LISP

I am trying to write a LISP interpreter in C#, so I started with a tokenizer. I haven't finished it yet (have to handle floating point numbers & symbols), but I already rewrote it two times ...
4
votes
1answer
206 views

Sorting Visitors

I started to write a code with top-down tests. My first version, grow to something like this: ...
17
votes
5answers
23k views

A custom thread-pool/queue class

I wanted a class which executes any number of tasks but only a certain amount at the same time (e.g. to download various internet content and keep the overall download speed at a good level). The ...
6
votes
1answer
3k views

Performance testing functions for wrapping an index

After asking this question, I decided to write a test and determine the fastest way to wrap an index (where my maxSize is always a power of 2). There are 3 ...
6
votes
2answers
132 views

How to reduce this archive detection function and make it supports wildcards

The function below is called to determine if a given file is the archive of another file. I'm also looking for a way to supports wildcards. For example if the original log file is serverw3c.log and we ...
11
votes
5answers
8k views

Nested if statements with 3 different parameters

I have a block of code below. The allDone() method at the bottom should only be run if the allCompleted == true. It should run ...
6
votes
2answers
10k views

Handling COM exceptions / busy codes

This code writes to Excel using the COM interface. The general issue is that any exception handling has to handle the "Excel is busy" exception. This occurs if information is sent to Excel quicker ...
12
votes
5answers
8k views

Image resizing class

How does this class to resize an image look? ...
12
votes
3answers
3k views

Random Number Generator Class

I've written an abstract class in C# for the purpose of random number generation from an array of bytes. The .NET class RNGCryptoServiceProvider can be used to ...
9
votes
3answers
3k views

Windows service for monitoring network interface changes

I'm currently working on several projects for my company to help reduce the amount of calls we have to deal with so we can focus on higher priority task, such as server resource reduction, etc. The ...
7
votes
1answer
6k views

Hypothetical SalesTax challenge

I have a small solution to the following hypothetical problem: Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty ...
4
votes
1answer
1k views

NHibernate (UoW) + cCommand pattern

I'm building an ASP.NET MVC app and would like some feedback for my way to query/execute statements to the ORM. I'm using Fluent NHibernate, and have mixed the UoW provided by NHibernate with a ...
15
votes
1answer
2k views

House number parsing

I have 2 functions used for parsing a house number and I think these can both be improved. Any suggestions? input: 45a - 47b or ...
10
votes
2answers
625 views

Loops for removing unnecessary lines

I have the following method: ...
12
votes
4answers
5k views

Calculating totals of a transaction

I am working on a project where I have to calculate the totals of a transaction. Unfortunately, coupons have proven to be quite an issue. There are four types of coupons: transaction percentage, ...
20
votes
5answers
12k views

MVVM, WPF Ribbon V4, with Prism

This weekend I've been having one heck of a time getting WPF Ribbon v4 working with MVVM and Prism (using unity). After much trial and error, I believe I have it working. I was hoping someone could ...
2
votes
2answers
10k views

Directory write permissions check

Consider the following method that I have for checking write permissions on a directory path: ...
9
votes
2answers
4k views

Animating the color of a line in WPF

I'm thinking about how to do a color transition for a line in WPF. I'm looking for this to be as simple and succinct as possible, and also the "correct" way in the WPF world. This is what I have, ...
29
votes
6answers
74k views

Parsing console application arguments [closed]

In our console app, the parsing of application arguments is done like so: ...
9
votes
3answers
2k views

ASP.net custom user control design

I've built a custom control that generates a tab style navigation. Each page might have different tabs to display, the aim was to modularise it so I can make global changes to all the tabs. TabMenu....