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
0
votes
0answers
65 views

Repeated generic type instances with generic method

I am working on a test infrastructure that can help developers in a web api project. Below I wrapped up assertion classes. However when we increase the number of verification classes then it will look ...
4
votes
1answer
172 views

recursive bin packing

For the company I work for we spec lengths of tube of varying lengths. For a given project let's say there is a list like this 100', 115', 105', 205', 195', 240', 240', 130', 180', 140', 225', 85' ...
1
vote
1answer
272 views

Polly AddPolicyRegistry() with factory and registered dependency injection service instances?

The Microsoft.Extensions method for Polly, to use policies via dependency injection, serviceCollection.AddPolicyRegistry() only allows to add already created ...
1
vote
1answer
97 views

Is the code properly documented/commented and does the code handle errors properly? [closed]

Is the code properly documented/commented? does the code handle errors properly? any suggestions to improve the code Help outline the object-oriented principles shown in the code. ...
4
votes
4answers
150 views

Better way to minimize array elements

I would like the array elements to minimize by preserving their orders. For example: 3,7,9,7 is given 1,2,3,2 is yielded ...
2
votes
1answer
79 views

Conway's Game of Life in a C# class. Is this sensibly factored and well tested?

I'm learning C# and have written the below class to encapsulate the game state of Conway's Game of Life and its update methods. I'm also learning about implementing light automated unit tests for ...
5
votes
2answers
853 views

C# code to check balanced brackets in a string

The code works, at least against the unit tests. But I would like input on how to refactor it. Or also maybe a way to do this without using temporary lists? I also have some special checks, like if ...
3
votes
3answers
185 views

Input Data Validator (good practice)

I work at an application that receives data from user and I try to write a validator function for it, but I'm not sure if this is the correct way to proceed. Example: The user will input a number (as ...
1
vote
1answer
897 views

Patch a JSON object using dynamic / ExpandoObject with System.Text.Json

Background Recently, I was making some updates to an "older" library that would handle PATCH-style modifications to an object that is persisted in a JSON format on our document-storage ...
0
votes
2answers
156 views

Using a IEnumerable where we have mostly only one item

I have a project where I publish and subscribe some data packages. Mostly those data packages are just one package, but sometimes (1 in 100) there could be more packages at one time (a lot more, like ...
2
votes
1answer
122 views

Generic PriorityQueue (Min-Heap) implementation

I was trying to implement a generic PriorityQueue in C#. Below is my implementation which works fine as per few test cases. Operations supported- Add: Adds an element Poll: Removes the smallest ...
1
vote
2answers
84 views

Efficiency of finding one database record using two ID's

I'm using C#, .NET Core and the entity framework and am currently working on optimizing various functions for better performance. One of the main functions is getting a calculation based on two ...
3
votes
1answer
115 views

C# screen recording

I'm developing a screen recording as an application log. The recording should capture a full HD screen at 25fps and on request it should save last 60 seconds of record. It is for embedded app, but the ...
0
votes
0answers
107 views

Unity C# CubeController script (jumping & moving the cube)

I'm kinda new to C# and Unity, coming from PHP/Python background. So I'm curious, if this CubeController script can be considered a bad code. I mean the coding style, performance, & overall Unity ...
0
votes
2answers
90 views

Finding items involving a particular category

I have a Item Service that is Called from an Asp.Net Core API Controller. The query gets all items in a particular category for display on an eCommerce web site (reactjs). The controller only returns ...
0
votes
1answer
65 views

Collecting data from list and then removing

I created this sample and wanted any advice on how to make this code; cleaner, more effective, just overall better! I am collecting the days data from the NegativeScheduleSentence model and ...
1
vote
2answers
69 views

Should I get Autofac to include classes that don't have a public constructor?

I have a Asp.Net API (.NET Framework 4.7.2). We recently updated Autofac to version 6.2.0. Autofac does not like some of our classes that don't have a public constructor. Example class: ...
0
votes
0answers
87 views

ThrottledStream

Need a ThrottledStream for a project, created the following class (still not tested), wouldn't mind some opinions on the code and suggestions on how it can be ...
0
votes
2answers
97 views

Find first non repeating char in a string

This is LeetCode question 387 if anyone is interested. The task is to accept a string and find the first char that appears only once, and return its index. If no char appears only once then return -1. ...
0
votes
1answer
54 views

Compare enum's and adding key word

I created this sample and wanted any advice on how to make this code; cleaner, more effective, just overall better! I need to add the key word and only if the ...
0
votes
0answers
45 views

Decompose URL components and store into a database

I'm writing a web crawler, and want to store the parts of a URL to a database using Entity Framework. Instead of trying to parse a URL into its component parts myself, I thought I could inherit from ...
1
vote
0answers
52 views

Simplify LoadLibrary/GetProcAddress helper for dynamic parallel DLL instantiation

Code is meant to be used in Windows .NET (tested with dotnet5.0) to dynamically load C library and return delegate of given function Code lower is result of my attempt to rewrite the ...
1
vote
2answers
84 views

Database dynamic query

I am working with C#, Npgsql, EF Core and Postgres. I ...
2
votes
2answers
212 views

Simple MemoryCache implementation for thread safe caching

After much prompting from this post I wanted to build a simple, in-memory, thread-safe cache. The only caveat (as far as I was originally concerned) was the need for two different absolute expiration ...
-1
votes
2answers
56 views

Factoring out common elements of an email transmission routine

I wrote some code and realized they share similar logic. I am struggling to share the logic and welcome any suggestions. ...
4
votes
3answers
217 views

Implement IDisposable correctly using object composition principle

Update at the end Is it possible to implement IDisposable pattern correctly while using object composition principle to promote code-reuse, reduce code duplication ...
4
votes
0answers
86 views

Simple Leaky Bucket Async and Low Footprint

I'm working on a simple Leaky Bucket algorithm. I have found a lot of samples on the internet, but something always bothers me. Most of them use Collections and DateTime to track current actions and ...
3
votes
2answers
145 views

Hangman game with only one word

This is a hangman game. The only word is 'word' and I am not planning on changing this since I am not going to publish this game or anything. If there is any unnecessary/bad code I need to get rid of, ...
1
vote
2answers
113 views

Configure service with user-related parameters in .NET Core dependency injection

I have service class which loads data from external service. This service needs API key which loads from db and unique for every user. ...
0
votes
2answers
114 views

is this a good way of using events?

So I'm currently trying to learn more about events using C# and the example that I just write seems a bit.. It seems a bit too much? I feel like you could accomplish the same thing with way less code. ...
1
vote
1answer
79 views

Copy vehicle information from JSON list to relational database

I am looking for suggestion to make my for loop iterate faster. With about 2000 rows of data. Below code is taking lot of time to run. So far I have followed various Stack Overflow answers. For ...
1
vote
3answers
112 views

Is this method for loading data in a C# application recommended?

Goal I'd like to understand if this current method, of loading data and inserting data into a database I currently use is to be avoided? Code I drafted a simple application that loads data and inserts ...
1
vote
2answers
225 views

Using multithreading to send multiple emails

I'm trying to send any number of emails in the shortest amount of time using .NET 5.0? I've been playing with something like the following but I am not sure if it is optimal or even correct as there ...
0
votes
0answers
84 views

Validation in Web Api layer or Service layer

I have this method in Web Api layer. ...
2
votes
2answers
92 views

Print Binary coded decimal Numbering of a given input number

Example 1 input:3 output:0011 Example 2 input : 15 output: 1111 in the below example code 15 is input. I have taken 8 4 2 1 as array for Binary coded decimal numbering this code is working as ...
0
votes
1answer
141 views

An async time-based rate-limiting semaphore for C#

This is a class that allows only limited number of requests to proceed per period of time. This is designed for use with external APIs that require such rate limiting, e.g. 600 requests per 10 minutes....
1
vote
1answer
108 views

Concatenating two possibly null strings with a separator

I have two strings (aggregatePath and pathFromParent), either of which might be null or an ...
0
votes
1answer
92 views

How do I optimize my GetChildren method

UPDATED I have a POCO class that is populated with _context.Categories.Load(): ...
3
votes
3answers
1k views

Printing rotations of an array 7 times

Loop through a given array 7 times and print the following output: int[] arr = { 9, 2, 7, 4, 6, 1, 3 }; ...
2
votes
1answer
81 views

A simple C# script that exploits PHP command injections

Back in 2016, I wrote a simple C# script that exploits a generic command injection vulnerability on PHP webapps, just for the sake of demonstrating that exploits can be written in .NET languages (due ...
2
votes
0answers
47 views

Embedded services using chain of responsibility?

I currently have a few "worker services" that wrap existing code to automate incredibly repetitive code. A few examples are: Method analytics. Iteration support. Exception logging. My ...
3
votes
1answer
63 views

Posts data to endpoint, trying not to use .Result on async methods

I have a Send method that is working but want to refactor a portion where it is using PostAsync().Result and ReadAsStringAsync().Result. I've never written async methods before and don't feel I ...
0
votes
0answers
59 views

Find the number of different submasses in a weighted string

I'm using a site similar to SPOJ (in Portuguese, so I don't think linking it will help), where I need to find the number of different submasses in a given weighted string. Through the use of an ...
1
vote
1answer
61 views
4
votes
1answer
115 views

More efficient way to create an ASCII maze using box characters

I've written a C# program to generate a random maze, and then draw that maze in the Console mode using box-drawing characters, e.g. ─ │ ┌ ┐ ┬ ┼ etc. It works fine, as below, but I'm not convinced that ...
1
vote
1answer
60 views

Best practices in regards to pass and deserialize data, when calling an API endpoint from a MVC-project?

In my application, I only show users their own data. For that, I need to know in the backend, which user is requesting data. I use the username for that and therefore, I need to send the username as ...
4
votes
0answers
84 views

License-holder editing application

I am building a WPF (Windows Presentation Foundation) application. And I'm trying to use the MVVM (Model–View–ViewModel) design pattern. It's fascinating and rewarding, even though it seems overly ...
1
vote
2answers
134 views

Combine elements of two lists into another list

I have two lists, I need to make another list with the same elements than the other lists. But elements on this list need the next sequence. list1[0], list2[0], list1[1], list2[1], ... and so on for ...
1
vote
1answer
76 views

How to continiously read data from a NetworkStream

So I'm currently learning about networking between a client and a server and I decided to try to write my own client and server. I implemented a way of receiving data and it goes as follows.. The ...
0
votes
1answer
126 views

Good practices for a class

I was working in a class, but there are a lot of things that I'm not sure if they're a good practice or not. For example, the => instead of the ...