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
5
votes
4answers
443 views

Thread-safe, Convenient and Performant Random Number Generator

I need to generate a lot of pseudo-random numbers in my software. I'm trying to create a elegant (syntax-wise) and performant class that would allow me to generate such numbers and perform other ...
3
votes
1answer
47 views

Sub-Middleware like options builder for an Asp.Net Core Middleware

Building upon a recent question I answered here Simple middleware pipeline builder (similar to asp.net-core) I came across a cross site question Stackoverflow: Adding middleware in Carter ...
7
votes
3answers
250 views

Convert HTML color to OLE

I have this function that takes a string which represents a html-color and returns the OLE value for it. If the string is null or empty or it can't be parsed, it uses a default value. However I have ...
0
votes
0answers
89 views

Bank ATM App Mockup - Implementing Domain-Driven Design (DDD)

There is a follow-up on this question. I have created this bank ATM app mockup which I believe implement some Domain-Design Design layered architecture, repository pattern and uow pattern, if not ...
7
votes
4answers
468 views

Rotate List by K places

I am trying to solve the question at :https://leetcode.com/problems/rotate-list/ Question: Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1:...
2
votes
1answer
45 views

Asp.net web API Nlog implementation with UnhandledExceptionHandler

I have an asp.net web api. I would like to log errors into a file. So I decided to use NLog. I already have UnhandledExceptionHandler so I added NLog as follows. I removed all the try/catch in order ...
3
votes
1answer
86 views

LeetCode #37: Sudoku as Exact Cover Problem solved using Dancing Links

Introduction I provide a regular 9x9 Sudoku solver, reducing the puzzle to an Exact Cover Problem. The algorithm used is Knuth'...
12
votes
5answers
716 views

Reducing contention in thread-safe LruCache

I was looking for a in-memory cache class, but couldn't find any in the .Net Core framework; luckily I found one already implemented here. I made some minor modifications to it and tried to make it ...
1
vote
1answer
59 views

Creating a ModelBinder to Sanitize user input in HTML format

I have a custom model binder which takes user input in HTML format, sanitize the HTML input by removing any script or XSS threat and returns the sanitized HTML string: ...
1
vote
1answer
67 views

Format the output of Color.ToString() to RGB values [closed]

The method that I wrote below functions properly; however, I believe that the code I wrote is not clean nor "good". I know the basics of regex but I do not know more advanced techniques as I am still ...
3
votes
1answer
80 views

Deserialize with LINQ query code in PageModel or PageView

Today I do something like this, where I get, select and sort some data and pass it on to the view (which loads a partial view) as a list of custom objects Page model ...
5
votes
2answers
186 views

LeetCode: Linked List Cycle

Challenge https://leetcode.com/explore/interview/card/top-interview-questions-easy/93/linked-list/773/ Given a linked list, determine if it has a cycle in it. To represent a cycle in the ...
6
votes
1answer
78 views

Custom HttpClient implementation for third part usage with sync/async calls

I'm doing an implementation of HttpClient that is built in a NET Standard project, which will be used as a base to build and process JSON requests/responses for a third part REST API. Client is built ...
2
votes
3answers
101 views

Simple middleware pipeline builder (similar to asp.net-core)

I created a simple middleware pipeline builder that I want to use for my frameworks. It is similar to how ASP.NET-Core middleware works and uses the same conventions: A public constructor with a ...
5
votes
1answer
153 views

TreeView class in WPF class

We are converting a WinForms application to WPF. so I need to accept the input like WinForms. As I don't want to change the logic of it. Ao I write the class for those its works. But the is not good ...
10
votes
4answers
2k views

AsyncDictionary - Can you break thread safety?

This class is an Async/Await wrapped Dictionary. Of course it doesn't technically implement IDictionary, but the functionality is basically the same as an IDictionary. It achieves similar ...
5
votes
2answers
152 views

Code Challenge: basic arithmic operations using only incrementation operator

Challenge Write a function that takes 2 integer operands left and right (both >= 0) and evaluates one of the following ...
4
votes
2answers
121 views

Reading Data from XML and storing them into SQL Database

I am developing a C# Windows forms application. In that, I am reading data from XML which is coming from a web API and storing that XML data into SQL database. I am able to achieve this. But my only ...
2
votes
1answer
83 views

Simple console application that changes text color based on user input

I am learning C# like 1 or 2 months and I wanted to know if there is any possibility to shorten this code. It is simple. It only changes color of the console text with ...
6
votes
2answers
74 views

Basic console calculator with four operations

I'm an absolute beginner in programming, and Today I decided to put my knowledge to the test and create a basic c# calculator. I've Done it, but now I'm looking for ways to Make it more shorter and ...
-1
votes
2answers
130 views

Union of two double arrays

I would like to merge two double arrays and remove from the result array approximately equal items. My two input arrays have ...
1
vote
1answer
82 views

Async Tcpwriter and Reader

I currently have a class that I want to serialize/deserialize messages sent to this socket. My plan was to use Task.WhenAny() to monitor 2 tasks (Either watch ...
5
votes
3answers
148 views

Collect reporting data for each possible combination of filter properties

Basically I'm looking for a way to improve my code, and avoid making a lot of if statements and parameter combination manually, 'cause I need to return a query result depending in the parameters that ...
4
votes
2answers
81 views

MVVM model letting the user select a directory in a testable manner

My application needs to let the user select a folder from somewhere on their local machine. Below are the interface and model pair that I have written to let the user do so. ...
2
votes
1answer
101 views

Simple, flexible, and thread-safe key/value memory cache

MemoryCache.cs ...
6
votes
1answer
76 views

Distinguish between different types of log items in a dictionary by their key

My logging adapter is using a dictionary for storing data that is passed from middleware to middleware. It is a one-liner: ...
11
votes
3answers
3k views

C# TCP server/client class

Goal My goal is to create a TCP server/client class that can be universally reused in various private projects. It should work stable and does not need to be super fast, but rather light-weight to ...
1
vote
1answer
53 views

Insert new middleware at the “right” location

I need to have my logger middlewares (nodes of a linked-list) in the right order so that they complement each other and work as desired. Any known middleware can be inserted anytime but the first and ...
2
votes
1answer
42 views

Delete redundant files in folder and db

I want to delete files from folder, which are not stored in DB, and DB records, which no more linked to files. My code: ...
2
votes
2answers
73 views

Logger adapter with a configurable chain of responsibility of middlewares

I use my own logger adapter that collects data into a dictionary: public class Log : Dictionary<string, object> { } This is really all I have. I then pass ...
4
votes
1answer
105 views

Optimizing Infinite terrain in Unity

I wrote a infinite terrain script which works! Saddly everytime the player moves a chunk it lags for a moment. I know my code isn't great but I'm here to learn why and get this working too :D <...
3
votes
1answer
36 views

Validating partially filled objects

The following classes are a simplification of an auto-generated code of an ORM (targeting Microsoft's Dynamics CRM): ...
6
votes
4answers
3k views

Create two random teams from a list of players

The task is to create two random teams from a list of players. Given an example of players = ["John", "Mike", "Alice", "Bob"] One example random team would be: <...
10
votes
2answers
1k views

Bouncing map back into its bounds, after user dragged it out

Users like exploring maps, seeing all that is around. However they tend to go too far. To prevent the map from getting lost to the user it bounces back into the set bounds, as defined by a rectangle. ...
3
votes
1answer
133 views

String.IndexOfAny(string, string[]) (lowest position for group of needles in haystack)

This is a follow-up to this question with bug fixes, question and code improvements from @dfhwze, @PieterWitvoet, @HenrikHansen, @t3chb0t. I am still hoping for an improved approach or algorithm ...
2
votes
1answer
47 views

Searching for matching shares

We're working on some code that involves validating certain UNC paths (specifically looking for things that appear to be subdirectories of other things), and we've gone back and forth on how to best ...
6
votes
2answers
271 views

Wrapping IMemoryCache with SemaphoreSlim

I need simple caching for one of my web-services so I use the Microsoft.Extensions.Caching.Memory package. I wrapped it with ...
9
votes
4answers
925 views

Filter search results by multiple filters in one operation

I want to filter search results by multiple filters at once. Is it possible to reduce the number of if statements? My code: ...
4
votes
0answers
96 views

Toggle any application feature ON or OFF - follow-up (v2)

This is the second version of my FeatureToggle service (see previous question). It still has the same purpose which is to help control whether a feature is ...
1
vote
0answers
71 views

E-Commerce Mock App in Console

I have completed this E-Commerce Mock App in Console C# which I used object to store the data to apply some C# OOP and LINQ techniques. Since it is just a mock version, features are reduced to the ...
0
votes
1answer
103 views

Is there a better approach to implementing Dapper with Mediatr?

I'm playing around with using Dapper with Mediatr in a .NET Core API. It's all working nicely. Its a learning project for me so I am just putting together a system that will hold user details ...
2
votes
1answer
50 views

418 I'm a TeapotServer for RESTful integration-tests

I created a test-server for RESTful integration-tests that I call TeapotServer because I like the status-code 418 that the ...
1
vote
3answers
48 views

Collecting data into objects and POSTing to 3rd Party API

I have a semi-working project currently and am wondering if I am doing this the wrong way or if there is a more efficient way. I will try to be as general as possible as to keep the point concise. ...
1
vote
2answers
174 views

Best C# implementation of IndexOfAny(string, params string[])

Follow-Up question .Net provides String.IndexOfAny(string, char[]) but not String.IndexOfAny(string, string[]). The existing ...
3
votes
2answers
89 views

Quaternion and Vector3 transfomation Math

hi I have made and AI for my enemies which works perfectly but a particular section of code is repetitive and I feel that it can be optimised ...
3
votes
2answers
40 views

Unity Custom InputManger for Cross-Platform Input

I have been working on my game for quite a while. one of its key features is cross-platform local multiplayer for example 2 players on 1 keyboard and some people on the controller. However after ...
4
votes
1answer
180 views

Difference between dates in Months and Days [closed]

I've written a function that returns how many Months and Days are between two dates. I want to respect calendar month boundaries, but I'm ignoring time as it's not relevant for my needs. I've got the ...
-1
votes
1answer
65 views

Using Dependency Injection through constructor [closed]

I am working on a asp.net mvc project. And I wonder if the setup of the project is correct. So just some advice Because I see this: ...
3
votes
1answer
113 views

Capsulation solution for byte arrays

In a part of the project, I had to implement a solution for encapsulation of byte arrays. We do use ssh and secure ports for socket connection but I was in need of an extra layer of protection against ...
6
votes
1answer
109 views

Shunting-yard Algorithm, implemented based on reference pseudocode

I have a lot of problems making this code look more elegant or to reduce its size. The algorithm is mostly copied from the pseudocode of wikipedia page. The difference is I prepare my string with the <...