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
14
votes
5answers
1k views

What’s your opinion on a Throw() method?

Lately I seem to run into a situation very frequently where I want to write something like the following: ...
11
votes
3answers
7k views

Searching a sequence for a pattern

I have written the following tiny extension method to help me when I'm working with sequences in which I have to find a pattern. ...
11
votes
4answers
3k views

Composite and Visitor patterns for tree-based survey functionality in C#

I have written some survey functionality for a project. Basically, a generic survey form that can be composed of sections and questions. I have a Survey class, <...
9
votes
3answers
568 views

Are these endless if statements a bad pattern?

I'm only asking because almost the entire code base I've inherited here, in C# and PHP, looks like this: ...
5
votes
1answer
645 views

Implementation of String.prototype.replace

With just a brief look over I can think of a large amount of changes that would improve the clarity but I would love to see how someone else would refactor this mess. Spec Code ...
4
votes
2answers
870 views

Custom UIElement Animations

Having fancy animations in WPF is quite nice so I tried to implement a generic fading animation for UIElements which I use for the Popups in my application. I was ...
11
votes
1answer
20k views

Get params from a URL

I want to write my own version of HttpUtility.ParseQueryString, because it is not included in the .NET Framework Client Profile. My code is pretty straightforward ...
8
votes
3answers
913 views

Stack Overflow like pager

I've been working on a Stack Overflow like pager for my personal use and I've gotten everything working pretty good. Some of my logic is a bit suspect though, especially the logic in the Pager class ...
17
votes
11answers
1k views

Reversing a string

I had this as an interview question, and the interviewer pointed this out. Here's what I wrote: ...
5
votes
4answers
281 views

Adjusting integer based on multiple elements in int array

I'm working with the following code which adjusts an int based on multiple elements of an integer array. I am wondering if there's a cleaner, easier-to-read construct for this: ...
6
votes
4answers
654 views

Is there a shorter way to write the following if statements?

I have the following code which uses four if statements to check the value in four different combo boxes. ...
5
votes
2answers
3k views

Generic BinaryReader/Writer Read/Write methods

I have an extension that implements all the BinaryReader/BinaryWriter in the Stream class. I want to create generic methods for them. Is there a nicer way of doing this? ...
7
votes
2answers
816 views

Scripting language parser

I'm making a scripting language parser and it's shaping up well but it takes about 30ms to do 30 or so odd/even checks. Profiler tells me that it's taking most of ...
11
votes
2answers
17k views

IIdentity and IPrincipal Interfaces

I've always seen the IPrincipal and IIdentity interfaces separately, but I haven't seen any compelling reason for it, so I have ...
10
votes
2answers
610 views

Left join query of user profiles

I have the following left join LINQ query that is returning the results that I expect, but it does not "feel" right. I need ALL records from the UserProfile table. ...
11
votes
2answers
76k views

Overwriting an existing file in C#

I just found out about the existence of this forum and it's exactly what I needed. I just asked this question on Stack Overflow and after reading the answers I came up with a code (not the one I ...
9
votes
2answers
8k views

Linking GUIDs to enums

While working with DirectShow, I came across the need to easily recognize different known 'sets' of GUIDs. E.g.: There are different GUIDs to indicate the possible time formats: ...
4
votes
3answers
3k views

Generating permutations and combinations

How can this code be improved? ...
7
votes
3answers
4k views

ThreadSafeObservableCollection of (T)

The idea here is to implement a simple, threadsafe, observable collection that clients can bind to, whilst background threads can update. Changes in the contained items raise the ...
8
votes
3answers
889 views

My Calendar generation code is slow. How do I improve it?

I have a model I have created to generate iCal and the necessary JSON for the jQuery calendar plugin Full Calendar to work. When I display the page, the JSON generation takes about 7 seconds. The ...
3
votes
3answers
510 views

Getting contact information

How can I improve this code? ...
15
votes
1answer
56k views

Verify which columns have changed in a datatable or datarow

I was asked to update a business rule to allow a specific column to be added. Because our datalayer uses only DataSets as an interface to the database that is what we work with in the business layer. ...
6
votes
1answer
195 views

Overlapping table fragments

One of my side projects is a table driven scanner/parser generator, in an effort to reduce the size of the transition table I represent the generated table A[x, y] ...
13
votes
7answers
8k views

Calculating compass direction [closed]

I have a heading, which is an integer and from it I would like to figure out the compass direction (North, North-East,...) and return the appropriate icon. I feel like I'm probably already at the ...
4
votes
1answer
512 views

WebMethod slower on execution than Windows application of the same code

I have developed an application that interacts with IBM ClearQuest. The problem is that when I run everything locally, such as, run the webservice local and then ASP page local, everything is at the ...
5
votes
1answer
428 views

Best way to locate Find on List<T> with reflection

I am locating the find method of List in C# with the single Expression type parameter I am using the following ( where T is a List, in my case a SubSonic IActiveRecord ) ...
11
votes
4answers
2k views

Filtering a recursive directory listing, discarding subfolders

I needed a function that would take a list of folders and removed all sub folders, so that only the top level folders stayed in the list. For example, given the list: ...
10
votes
4answers
5k views

Circular Bounded Queue using C#

As an exercise, I have implemented a circular bounded queue using arrays as the base data structure. I'd appreciate it if someone can review it for me. ...
7
votes
1answer
8k views

Multithreaded host pinging application

I'm working on a network monitoring application, that pings an unknown number of hosts. I've made a class PingHost with a function ...
6
votes
2answers
3k views

Best practice instantiating generic delegates and accessing property getters

I want to create delegates to access properties of different objects without knowing them in advance. I have the following definitions ...
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
489 views

Is this a valid loop?

I have the following code: ...
8
votes
2answers
16k 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 ...
52
votes
4answers
151k 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
538 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
745 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
21k 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
509 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
716 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
247 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
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. ...