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
45
votes
3answers
6k views

Benchmarking things in C#

I needed a better way to benchmark code, because, well, rewriting the same benchmarking code every time I need it is just...well...unpleasant. So, here's a class which does just that, it runs an <...
37
votes
4answers
7k views

“How are you spending your time on the computer?”

I've made a Windows Forms application to track all of the processes running on my machine and it also saves the time an application is "active", an active application is the one that is on focus ...
28
votes
4answers
4k views

Snake Game in C#

Recently, I have been trying to learn SOLID principles. I learn the best through criticism. I made this snake game in Unity with C#, where I put my current understanding of SOLID principles to test. ...
27
votes
9answers
57k views

Check a string to see if it is a palindrome

I have written two code pieces to check if a given string is a palindrome: ...
23
votes
7answers
5k views

Sales Tax Problem, rejected for not being up to their standards

I applied for a Junior .NET Developer Position recently, and I was asked to solve a problem (Sales Tax Program) and I was rejected for some reason. The question goes like this: Basic sales tax is ...
17
votes
2answers
863 views

Enforcing string validity with the C# type system

One of my biggest complaints about .NET is that there's no way to guarantee a string match a certain type in the type-system. Note This started as more of a proof-of-concept than a real usable ...
5
votes
1answer
8k views

Unit of Work with Repository Pattern MVC 5 & EF 6

I put together a sample of how I am using the Unit of Work & Repository pattern based on my understanding. Can anyone please let me know if I am implementing this the correct way? If I am not, how ...
82
votes
9answers
7k views

Mutually exclusive properties

The question is probably quite simple, but I would like to hear what drawbacks we will have with our code. So, we had a simple implementation and interface for it: ...
14
votes
7answers
1k views

Binary Heap where a comparison delegate is used

I currently have a Generic Implementation of a BinaryHeap. It must be able to maintain it's integrity with elements that may or may not implement ...
13
votes
2answers
10k views

Do I need ninject when implementing DAL with generic repository and unit of work?

I am implementing generic repository and unit of work for the first time. I would be glad if someone can correct me if I am doing something wrong here. This is how I communicate with my DAL from ...
36
votes
4answers
7k views

OO coffee dispenser

This picture inspired a contest between a few of my friends to rewrite this code in more proper OO style. This is what I have come up with. Any thoughts: ...
24
votes
4answers
38k views

Type-safe Dictionary for various types

Assume the following situation: you have an object that can store any object based on a key (basically, IDictionary<string, object>). You want to store ...
23
votes
5answers
15k views

Single line FizzBuzz solution in LINQ

Now FizzBuzz itself isn't a big challenge but I agree that it can be a good tool to see if someone can code or not. I wanted to practice my LINQ a little bit so here's my single line FizzBuzz ...
12
votes
3answers
37k views

Tic Tac Toe game in C#

I wanted to get into C#, so I tried making a simple game. My first language was Java, so it was pretty easy to get the hang of C#. I know that there are multiple other posts of Tic Tac Toe games, ...
10
votes
3answers
27k views

Function that convert decimal to binary

I have a function that converts an integer to its binary representation. I am wondering if there is any way I can improve the function. ...
25
votes
7answers
9k views

Is it bad practice to increment more than one variable in a for loop declaration?

While coding today, I came across the following loop in C#: ...
24
votes
3answers
4k views

A simple mastermind clone

I made a simple Mastermind clone and I'd just like some tips on what I could do better/different solutions for what I have already coded. If you're wondering what mastermind is, there are, for the ...
23
votes
4answers
29k views

Simple Age Calculator

I have written an age calculator that takes a birthDate as input. I'd like a general review of this. I'm especially concerned about the ...
14
votes
5answers
3k views

Mini RPG equipment code

what do you think about this code? It's upgraded version of: Classes representing items in an RPG game ...
14
votes
2answers
4k views

Repository and Controller tests

Lately I have been researching how to best unit test an EF repository and given a properly tested repository, what to test in the controller. My research did not reveal many sound examples or ...
14
votes
3answers
19k views

Timing out a method

I have created this extension method that can be used to run a function with a timeout applied to it. Is this a sensible way of doing it? ...
12
votes
1answer
22k views

In-memory cache implementation

A few years ago I required a lightweight in-app-in-memory cache. Requirements: Time complexity O(1) for individual element read/write access Space complexity ...
12
votes
1answer
3k views

Fluent API of a Role based access control implementation

I am trying to provide a fluent API for authorization based on roles. As you will see I separated my implementation in two related Interfaces the Session and the Query. The session provides all roles, ...
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 ...
7
votes
4answers
12k views

Improvements to a ViewModelBase

Every time I go to write an MVVM application (say every 4-6 months) I rewrite my ViewModelBase class. This is for a range of reasons but let's say either I don't ...
7
votes
2answers
19k views

C# - Entity Framework + Repository pattern + Unit of work pattern

After hours spent on reading various articles about correct way to implement Repository patter & UoW patters I finally think that I have managed to create (hopefully good) implementation. The ...
4
votes
5answers
16k views

Stack implementation in C#

I started to learn data structures. How can I improve my implementation? For example, I don't now how to push 0 or how to realize my own ...
3
votes
3answers
6k views

ASP.NET core proper way to work with the repository pattern

I am currently working on my first ASP.NET Core MVC project with a repository pattern. I have gotten it to work but I wonder if there is a better way to solve this. In this case I have an ...
3
votes
1answer
3k views

Is this a workable UnitOfWork with Repository pattern design?

I am building a web app with using UOW and Repository pattern. I have seen many different samples and found each one to be different, so not sure which is the correct way to go. I have a basic ...
33
votes
11answers
2k views

Too many loops in Drawing App

I have a method that has a lot of loops: ...
28
votes
2answers
102k views

Integer to Alphabet string (“A”, “B”, …“Z”, “AA”, “AB”…)

So this question is prompted by two things. I found some code in our source control doing this sort of things. These SO questions: https://stackoverflow.com/questions/297213/translate-an-index-into-...
15
votes
6answers
14k views

Determine if an image is opaque or transparent

I wrote an extension method defined for the Image Class (System.Drawing) — which returns a Boolean value telling you if the image is opaque or transparent. The color of each pixel in the image is ...
15
votes
7answers
13k views

Given two int values, return the one closer to 10

I did the exercise below to practice. Can someone help me to shorten or improve the code in a simple way for a beginner? The exercise is: Given 2 int values, <...
14
votes
1answer
104k views

Reading data from Excel sheet with ExcelDataReader

Objective: I want to import an Excel file, and read the rows of certain columns. For this, I use ExcelDataReader. I've implemented a low-level class called ...
14
votes
4answers
29k views

A TaskScheduler that always run tasks in a specific thread

The following is an TaskScheduler that always run tasks in a thread it maintains. When created, a name of the thread was specified. Once you schedule the first task,...
11
votes
6answers
3k views

Too Many Staff Meetings

I recently got in really deep on a question to find the overlapping meetings and I rewrote a bit of the code and added some things to the code, and I would just like a general review of my code and ...
9
votes
6answers
2k views

BaseRepository for EF6

Is this okay? ...
7
votes
2answers
10k views

File Async writer using Tasks or new features of .Net 4.5?

This code was posted as answer to Async file writer in .Net 3.5. How would it be done better using Tasks or new features in .Net 4.5? ...
5
votes
1answer
16k views

Polling loop to run in a background thread

I came up with the idea of a small utility class that will poll some delegate until the response received meets some condition, upon which it will notify the main thread which can take the appropriate ...
24
votes
2answers
5k views

Hooked on Windows

The code that was just added to Rubberduck will allow us to set up hotkeys for our features, so that next release, Ctrl+Shift+R brings up the refactor/rename dialog, for example. The requirements ...
16
votes
4answers
38k views

Regex to first match, then replace found matches

In my C# program I am using Regular expressions to: Loop through a list of possible words in need of replacing. For each word, to find out if a string I am given has any matches. If it does, I ...
15
votes
3answers
2k views

RPG Character Builder

This implements a builder pattern for RPG game characters. Is this a valid builder pattern implementation? There are 3 characters: Paladin, Wizard and Elfo. There are 3 items: Potion, Sword, Arch. <...
14
votes
3answers
791 views

Interpreting Brainfuck code to C#, then compiling to a .exe

This is a C# programme to convert Brainfuck to C#, then convert that C# to a .exe. So basically, it's a Brainfuck to Windows exe file converter/compiler. This requires any of: .NET 2.0 in C:\Windows\...
14
votes
1answer
2k views

Ambient Context

What do you think about this way to make logging available across the application without passing log object around? Let’s say we have something which allows us to ...
12
votes
3answers
10k views

Extension method replacing elements from an IEnumerable<T> collection

Today I needed a method to replace all 'x' values in a said collection. Since there is no such method by default in .NET I wrote my own: ...
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 ...
11
votes
1answer
2k views

Problem: Traffic Flow

I have the following task: The Shallow Alto city council has organized a special committee to review the traffic situation in the city. Despite the fact that there is nothing whatsoever to do ...
11
votes
3answers
9k views

Verify if meeting datetime ranges overlap in an IEnumerable list of meetings

I have an assignment where I need to find out if any of the meetings in a list of meeting class object have overlapping datetime ranges. What I have so far is this: ...
10
votes
3answers
5k views

Encapsulating common Try-Catch code. Is this a known pattern? Is it good or bad?

In an effort to reduce code duplication, I often use and have used this style to capture handling of exceptions on a boundary of an application: Given the following extension methods: ...
9
votes
3answers
249 views

Matrix reloaded … or reversed

I have written this Matrix class after posting an answer to Malachi's rags-to-riches question. The initial purpose had been to reverse a matrix represented by a 2D ...