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
8
votes
4answers
701 views

Selective updates to immutable types

There is a way in Scala to selectively change a field while making a copy of the immutable object: ...
8
votes
1answer
156 views

Normalize whitespace and describe string

The most valuable of all talents is that of never using two words when one will do. Thomas Jefferson. NuGet and GitHub I found it useful to automatically normalize string content a little bit and ...
8
votes
3answers
683 views

MIST - auto-implemented, attribute-driven .NET notification mechanism

I have a component that I've used myself for quite some time, the short story is that it automagically implements a property change notification mechanism (very useful for implementing ...
7
votes
3answers
1k views

Fluent Validation of Objects

Inspired by this question by t3chb0t and as an elaboration of my own answer, I have written the following solution. My goal was to reduce complexity both in implementation and use. Eventually - I have ...
7
votes
1answer
270 views

LINQ Provider: Supporting Projections

Up until recently, my LINQ-to-Sage provider didn't support projections, so the client code had to explicitly "transfer" to LINQ-to-Objects, like this: ...
7
votes
1answer
168 views

Cascading a property for a game

I'm trying to write a simple game. I have an Entity object, that has a list of Sprite objects. Each ...
6
votes
1answer
698 views

Leetcode 54: Spiral Matrix

Problem statement Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [   [ 1, 2, 3 ], ...
5
votes
5answers
13k views

Quick sort algorithm

Is this algorithm good or can I improve it? ...
5
votes
3answers
450 views

Nested switches vs domain specific parser

I'm working on an application which has a defined and immutable (for our purposes) communication protocol. One of the features is that users on the controlling terminal can enter text commands that in ...
5
votes
2answers
503 views

Console Application Customizer - GetBootstrap v2.7.0

GetBootstrap v2.7.0 Update from GetBootstrap v2.5 I add a progress bar feature in my console application customizer and update the #region grouping and convert the ...
5
votes
2answers
16k views

Implementation of an asynchronous TCP/UDP server

I am trying to implement a TCP/UDP server so all I have to do is something like this: ...
4
votes
2answers
149 views

Detecting a certain amount of violet in an image - follow-up

I'm trying to write a program to detect the colour violet in an image.As discussed in my previous Post @Johnbot suggested that I try using HSL / HSV to determine if the pixel is some shade of violet. ...
4
votes
4answers
5k views

How to know when I need to use get/set and when not? [closed]

I have read several articles using Google, but I somehow do not really get what they do, and when it is needed. Below I'll give 2 pieces of code, in one using get/set and in the other without, and ...
3
votes
1answer
295 views

Tic Tac Toe implemented in WinForms using Model-Viewer-Presenter Pattern

I had to build a 2-player Tic Tac Toe app in C# w/ WinForms for a pre-interview screen. I got past that stage successfully and the app works flawlessly, but I would like to get some feedback. ...
3
votes
2answers
14k views

Parallel Task Queue that runs in sequence

I'm in need of a solution that runs constantly incoming requests in a per-resource sequence, but parallel in general. The use-case: Many clients connect to a server and start issuing work. The work ...
3
votes
1answer
987 views

JSON serialization helper class

I wrote this class to one-line all my JSON serialization, and I'm curious of any input on it. It's only responsible for serializing/deserializing any type to/from JSON. The comments and code are ...
15
votes
7answers
2k views

My Rational struct, version 2

I implemented most of the suggestions from Version 1. Thanks to all who took time to review and offer really good comments. ...
15
votes
4answers
540 views

Entities and the things they do

So I have an Entity, which is responsible for providing an abstract base for other objects to inherit. (Like Actor objects, ...
15
votes
3answers
1k views

Simulating gravitational attraction between bodies

I'm currently working on a simple simulator in Unity3D that involves planets orbiting each other. I wrote the following C# script to do that. It uses the classic gravitational formula, $$F=G\frac{...
14
votes
3answers
7k views

Hello There Calculator

The open source project I work on uses Antlr4 pretty heavily, but I don't know much about it. I thought I would use April's Community Challenge as an opportunity to learn something about grammars, ...
14
votes
7answers
26k views

Checking if two strings are an anagram

This is my sample code to check if a given pair of strings are anagram or not. ...
12
votes
1answer
32k views

Small C# Pong game

I recently made a 140-lined Pong game in C# using forms. Since I'm very new to programming, I'd like if somebody would please read it and give me advice on where to improve, what you believe I did ...
12
votes
2answers
393 views

Single instancing class

I have created the following class that is used to provide single instancing functionality to an application. It is called like: ...
12
votes
2answers
9k views

BackgroundWorker vs TPL ProgressBar Exercise

I wanted to fiddle around with the BackgroundWorker and Task classes, to see how I would implement a given task with these two ...
12
votes
2answers
3k views

Implementation of the Strategy Pattern using an interface, abstract class and delegate

The following class was designed to help create a more detailed error message than what's provided by the repository when a user tries to insert text into a column that is > the column max length. The ...
11
votes
2answers
852 views

Walking Git History

I've recently taken an interest in analyzing commit history to find files with a lot of churn. The idea being that high churn files are likely targets for refactoring. Right now it's a simple console ...
11
votes
2answers
2k views

BinaryTree<T> written in C#

I've written a binary-tree using .NET Core 3.0. What can I do to improve my coding style? ...
11
votes
3answers
4k views

Structure to ByteArray Extension

I have a need to turn various structures into byte arrays to be sent over serial port to another machine. I created generic extensions to turn any structure into a byte array and from a byte array ...
11
votes
2answers
2k views

Poker Hand Evaluator, take 2

This is following up on my previous attempt, which was admittedly done fast and not-so-well. This code attempts to allow comparing two poker hands to determine a winner, not only evaluating a given ...
11
votes
6answers
5k views

Calculate fingerprint for an object

I need to store serialized objects in a database and I want to avoid duplicates. In order to be able to tell wheter an object is already stored, I have to compare them somehow. My idea was to ...
11
votes
1answer
6k views

Timer to poll a Delegate

This is a followup to this where I was initially using a single thread and Thread.Sleep() to poll a delegate. I was recommended to use a timer and the ...
10
votes
2answers
10k views

Circular Buffer Implementation

I'm implementing a "live analytics" feature and I need to always keep track of the last N items that were added to a collection so I figured I'd make a circular buffer since I couldn't find one. I ...
10
votes
4answers
61k views

SerialPort class for a library

I've been working a lot recently with SerialPort in C# and I've come up with the following class as part of a class library I'm working on for another program. My question is, are there any more ...
10
votes
5answers
10k views

Checking if a string contains vowels

I'm just learning C# and wrote a little program to check if a word contains vowels. It's fairly simple and not to exciting but I would like to know if there's easier ways to do this: ...
9
votes
1answer
2k views

Database abstraction layer for multiple providers

There's been a lot of questions lately about database provider and repository design especially without entity framework or alike. I thought I try myself to create a reusable framework for this kind ...
9
votes
5answers
1k views

Find first non-repetitive char in a string

I just had an interview for the C#.NET developer position and I was asked to find the first non repetitive char in a given string without using any library functions but just the basic constructs of ...
9
votes
1answer
46k views

Writing to file in a thread safe manner

I'm writing Stringbuilder to file asynchronously. This code takes control of a file, writes a stream to it and releases it. It deals with requests from asynchronous operations, which may come in at ...
9
votes
2answers
5k views

Parsing cron expression

I'm building my own scheduler and I took the first step by creating a cron expression parser. To test it I used this expression to cover each case. (I guess the 52W is wrong but this doesn't matter ...
9
votes
2answers
2k views

Optimize a generic foreach method that converts Datatable to my object using Reflection

I need to optimize this code block below, this method converts a Datatable object to the Object that I am passing by parameter, in another words, this serializes the object, however I have to run this ...
9
votes
4answers
38k views

Converting the DateTime to and from Unix epoch times

I'm currently working on a thing I needed this feature for. Essentially, these are two methods to convert DateTime objects to and from the Unix epoch time (two ...
8
votes
1answer
318 views

Caching data by using the result of first running operation

My code handles concurrent requests by waiting for the result of an already running operation. Requests for data may come in simultaneously with same/different credentials (including empty credentials)...
8
votes
3answers
1k views

Detecting a certain amount of violet in an image

I need to be able to detect if an image has the colour Violet in it. (Follow-up) The problem that I'm facing is that there are quite a number of shades of violet that it seems almost impossible it ...
8
votes
3answers
2k views

Leetcode 56: Merge Intervals

Problem statement Given a collection of intervals, merge all overlapping intervals. For example: Given \$[1,3],[2,6],[8,10],[15,18]\$, return \$[1,6],[8,10],[15,18]\$. My ...
7
votes
2answers
43k views

First program: a simple calculator

I'm reading this very good book, C# Player's Guide, to learn C#. I then plan to make video games with Unity 3D after. In one of the chapters, he asks us to make a (really) simple calculator as a ...
7
votes
1answer
4k views

Locking during cache population

Here I want to lock when populating a particular cache object, without blocking other calls to the same method requesting Foos for other ...
7
votes
3answers
2k views

Elevator Interview Problem OOP - Revised

Elevator program code challenge, revised: Can someone please critique my Elevator problem - I wanted to use OOP principles and coding standards. Also, does logic make sense? ...
7
votes
1answer
185 views

Integrating Unit Testing functionality into an old COM-based IDE

My UnitTesting add-in works nicely, but it has dependencies on a Reflection and a System add-...
6
votes
2answers
11k views

What is the 10001st prime number?

Project Euler problem 7 says: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? I believe that my code is ...
6
votes
1answer
2k views

Parallel Job Consumer using TPL

I need to provide a service (either a Windows Service or an Azure Worker Role) which will handle the parallel execution of jobs. These jobs could be anything from importing data to compiling of ...
6
votes
2answers
9k views

Async and error-handling

I need to delete a file when an error occurs during loading it. It is not allowed to perform async operations in a catch block. This has lead me to writing the following code: ...