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
15
votes
2answers
5k views

Dealing with Entity Framework's lack of enum support

I'm using Entity Framework Code First CTP 5 for a current project. There is still no enum support in Entity Framework so this is what I am using in order to avoid having magic numbers or casts ...
10
votes
2answers
490 views

Is this a valid loop?

I have the following code: ...
8
votes
2answers
17k views

Checking if an event is not null before firing it in C#

I often see this for custom events: void InvokeCustomEvent(EventArgs e) { var handler = CustomEvent; if (handler != null) handler(this, e); } But is ...
54
votes
4answers
157k views

Comma delimited string from list of items

Is there a simple way to create a comma delimited string from a list of items without adding an extra ", " to the end of the string? I frequently need to take an ASP.NET CheckBoxList and format the ...
9
votes
3answers
545 views

Cleaning up a redundant switch statement

How can I use a more generic method to clean up the redundancy in the switch statement below? I'm working on an ASP.NET webforms app that will have five identical user controls that I will be showing ...
6
votes
1answer
252 views

Suggest how to extract method

I have 3 methods(* BindingSource are bindingsources, context is data context, cache* -are some List for cache operations): ...
3
votes
1answer
763 views

Retrieving context menus from the form

I have the following code which is a private method inside the form and retrieve all context menus from the form. I feel, that it is not that concise as it should be. Would be grateful for any ...
5
votes
3answers
483 views

Similar event handlers for buttons

I have 3 simple similar event handler functions that I would like to refactor. Any suggestions? ...
9
votes
3answers
22k views

Generic advanced Delegate.CreateDelegate using expression trees

As far as I know the standard Delegate.CreateDelegate() which allows to create a delegate by using reflection, doesn't allow doing something as follows when the ...
7
votes
3answers
513 views

Moving to other game states with ScreenManager

I'm creating a game in C#, and I've created a ScreenManager so I can call ScreenManager.MoveToScreen(typeof(ScreenClassHere)) ...
3
votes
1answer
724 views

Shader parameters in managed OpenGL

I'm working on a managed OpenGL game engine for C#, and here's how I'm managing shader parameters (uniforms). Is it "alright"? Could it be better? I'm a bit unsure about using generics in this case. <...
5
votes
3answers
3k views

Elegant approach to building unordered list from XML

How can I improve on this rather ugly method that builds an HTML unordered list from an XML file? I maintain a collection of ASP.NET webforms that all need to pull in the same site navigation as our ...
2
votes
3answers
263 views

Majorly refactored my code, is this good enough?

...
1
vote
2answers
12k views

Reading file contents into a string

Option 1 - nice and simple ...
11
votes
2answers
25k 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
664 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
432 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
802 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
915 views

Capitalize words in sentence

...
11
votes
4answers
758 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 ...
233
votes
14answers
39k 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
3k 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
510 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
62k 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
669 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
329 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
293 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
592 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
207 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
133 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
9k 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
11k 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 ...