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
1
vote
2answers
66 views

Generic Implementation for Paging Class in c#

I have created one generic Page class for store paging information with its data. The class look like: ...
1
vote
2answers
123 views

Calculating a page size to execute a subset of jobs at a time

We have to execute a bunch of jobs , each have an ID and they are passed on to the processor . Since the processor can only work on a limited number of jobs (denoted by ...
5
votes
1answer
115 views

Testable REST API Client

GitHub and NuGet Allows to invoke public/private REST API just by defining an interface. 200 lines of C# in total. Demo Sample API is publicly available at http://jsonplaceholder.typicode.com: <...
4
votes
1answer
83 views

Prime Number Table, i.e. List<int>

I have written many variations of the Sieve of Eratosthenses, which is the fastest way to generate a large collection of primes. (@slepic asked for clarification, which I provide in an answer below. ...
5
votes
1answer
53 views

Strictly typed dynamic proxy to call dynamic object

GitHub and NuGet I would like to use DynamicObject derived types to execute REST API/NodeJS module calls. It would also help to capture API shape in a strictly typed manner, so here comes an ...
4
votes
0answers
74 views

Production planner

I have a class that creates an object, PlannedYear, and then passes that object into several other classes to manipulate it. The ultimate goal of the class is to ...
0
votes
1answer
120 views

Cancel Long Running Query using Cancellation Token AND SqlCommand.Cancel()

I need to be able to cancel a long running query programmatically through our application. The code below will kick off a long running query and give control back to the main thread. At any ...
1
vote
0answers
31 views

Next Bigger Number with the same digits Solution [closed]

Please review my code. I would love to know what improvements I can make in terms of efficiency as well as readability. Thank you! ...
6
votes
1answer
168 views

Minesweeper Implementation c# winforms

I made a minesweeper game in c# as a method of studying, and right now its fully operational. I tried to use inheritance, recursion, and other fundamentals as clean as possible. Also I tried to use ...
5
votes
2answers
130 views

Network IP packet tracker

this is my code for watching IP packets. I am doing project for my self because I want to learn how this things works. This is my IP Packet class: ...
-2
votes
1answer
80 views

Make foreach with nested-if's more compact and readable [closed]

Code works fine. What I'm looking for is C# syntax to make the foreach loop below more elegant, compact and readable. The foreach with the multiple if's takes up ...
1
vote
0answers
82 views

C# trading indicators calculation

I have a stream of stock prices and I want to design indicators calculation for them. Indicator can be imagined as an additional chart displayed next to the prices chart. More specifically, it is ...
5
votes
2answers
125 views

WPF/C# - Rock, Paper, Scissors Game

I have made my first little project using C#/WPF - a simple rock, paper, scissors game. I am happy with the functionality and look but I would love to know how a more experienced programmer would ...
3
votes
0answers
44 views

Manage independent script component architecture in unity

I have different set of objects where I have to show Name UI and Menu UI using OnMouseDown and on OnMouseEnter Events. Remember ...
4
votes
1answer
100 views

Mapping Excel rows to model

I have an Excel file and each row within the sheet represents an employee's data. I am trying to find a cleaner way to map each row to a model object: ...
3
votes
2answers
100 views

Validate if either or both of two fields has been provided, as a ValidationAttribute

I have a binding model where at least one of two fields must be specified. I can't just set them both as [Required]. Here is what I came up with: ...
3
votes
1answer
75 views

Iterate through Expression Tree

I'm trying to build an iterator (enumerator) that can select specific elements in an Expression tree by traversing the tree and deferring further iteration until ...
5
votes
1answer
92 views

RPG Parser routine

I wanted a routine that would allow me to use the NumPad keys for directional commands. 1-9 for each compass point, - for up and + for down. If one of these keys are not pressed, it would act as a ...
7
votes
1answer
121 views

Generating Infinite Procedural Terrain Using Command-Line Graphics

Continuing my trend of (ab)using the Windows command-line interface to do fancy graphics-related things with my command-line-graphics library, CLIGL, I've created an infinite procedural terrain ...
2
votes
1answer
124 views

Strictly typed JSON representation

I would like to be specific in parameter types or write something like this: Test test = (Json<Test>)"{\"age\": 46}"; Where: ...
-1
votes
1answer
126 views

Naming convention when converting between types? [closed]

If I have a method where the input is a list of integers, and within that method I convert the list to a list of strings, what is the best way to name the new list? For example (using ...
5
votes
0answers
87 views

Using ClientFactory-pattern with Dependency Injection

I initially posted this on Stackoverflow but was recommended to try here instead. I'm looking for feedback on why the implementation of my ClientFactory is probably bad, from a Dependency ...
3
votes
1answer
75 views

Refactoring code to implement clean code using repositories

I've been refactoring code today, try to implement a clean code setup using repositories. Is this on track? Can any improvements be made? ...
7
votes
2answers
325 views

Chaining statistics in a dice queue

I've previously came up with the question "Rolling dice in a method chain" and was interested if this could be improved. It could, so now I have this class: ...
7
votes
3answers
462 views

Averaging quaternions

To compensate for shaky hands when holding a mobile device I'm averaging the rotation of the phone over the last n (in this case 16) frames, and setting the camera'...
0
votes
1answer
67 views

Mixing Async and Sync in same HTTP request ASP.NET Core C#

Is it bad practice to mix async and sync call in same ASP.NET core API call? For example, in following code method CropBlackBroderOfAnImageAsync is an async method....
4
votes
1answer
55 views

WPF Custom Control with a Single ObservableCollection DependencyProperty

I'm learning how to write WPF custom controls. My example, NameLister, exposes a single DependencyProperty, Names, of type ...
2
votes
0answers
54 views

Unit testing the code that is written using repository pattern

I have implemented my business logic using the repository pattern. I have an Approve method in my controller. I am calling the service method ...
4
votes
2answers
141 views

Is there a way to avoid repetitive code in the following switch statement?

I have the following method, which is called every time the user clicks a checkbox within a Datagrid. Each column within this ...
2
votes
1answer
61 views

String template using concrete implementation instead of external library

Q1: What are the benefits of using any external library e.g Antlr3.StringTemplate? Q2: What are the potential issues with the below implementation? ...
8
votes
3answers
736 views

Rolling dice in a method chain

I've been writing code for nearly 40 years now and am still not too old to learn and understand new things. Right now, my focus is a bit of OO with functional programming combined and C# with Linq is ...
3
votes
3answers
198 views

A prime numbers enumeration optimized for speed

I know there is this thing called "Eratosthenes" but that requires the allocation of a large array while I want to find (small) prime numbers fast, yet without needing too much memory. So I wrote ...
17
votes
6answers
6k views

Solving Fizz Buzz using LINQ in C#

The Fizz Buzz challenge is well-known and should not need any explanations, right? I recently got a bit bored and came up with a solution for Fizz Buzz using LINQ and even asynchronous code and a lot ...
6
votes
2answers
143 views

Find the largest sum of any slice inside an array

This is the 26th exercise I solved from Codility. It scores 100%. I think I improved a lot since I started with this lessons, around 2 months ago, but still every post I made had things to be improved....
3
votes
3answers
84 views

Method for rotating [Flags] Enums

I'm working on a method to rotate System.Enum values that have the [System.Flag] attribute set. I've tried to think of cases ...
8
votes
1answer
199 views

Mars Rover Challenge in C#

This is my code for the Mars Rover Challenge, would be nice to get some opinions on it. More details about what the Mars Rover challenge is can be found here: https://code.google.com/archive/p/...
12
votes
2answers
1k views

Given an array A[] and a number x, check for pair in A[] with sum as x

LINQ/FP style: Assert.IsTrue(new[] {1,2,3}.AnyPairSum(4)); Where: ...
4
votes
0answers
96 views

Implement Sudoku using LINQ

WiKi and GitHub. Sudoku rules are being checked with the following tests: ...
3
votes
1answer
63 views

Count the number of leaders in both sides (slices) of an array in C#. Codility EquiLeaders task

EquiLeader A non-empty array A consisting of N integers is given. The leader of this array is the value that occurs in more than half of the elements of A. An equi leader is an ...
4
votes
1answer
153 views

Is this code SOLID?

I have recently learnt SOLID patterns and started practicing them. I did follow all the guidelines but was thinking for a final review from experts if possible. What is the code about ? It encrypts ...
8
votes
0answers
141 views

Geode Contents Predictor StardewValley Mod

The goal I really enjoy the game Stardew Valley. One of the things that the game has are geodes. Much like real-world geodes, these geodes can be cracked open, and you can find some fun things inside....
7
votes
2answers
134 views

Finding a way to make this 2D loop faster?

So I'm creating a unity3D game with infinite terrain and it's going great! However, I'm struggling with a specific but important part of the 'infinite' terrain. Make the terrain actually infinite. I ...
1
vote
2answers
88 views

Check if dictionary keys are declared, if not leave them as true

I've tried with: dictionary.TryGetValue("MoreDetails", out bool MoreDetails); but if the key is not present it defaults to false, and I need it to default to ...
1
vote
1answer
167 views

Dictionary for locking on unique identifiers without leaking memory

The following code is greatly inspired on this question and this answer. That answer works quite well for a specific case, but I was looking for a solution that: could be used more generic (so I can ...
4
votes
1answer
218 views

Dapper “async” and “await using”

I have been building my data access layer and performance is key for the project I am working on. I have chosen to use Dapper and dotnet core 3. I want to make use of the latest and greatest async ...
4
votes
0answers
39 views

Imrpove code for loading components [closed]

It is a usual pattern when creating a new component, to load references to other components of the same GameObject. Here is an extreme case: ...
0
votes
1answer
79 views

Clarification on “if” statement - Digital Clock Tutorial C# [closed]

I am building a digital clock in C# after watching a tutorial. The code in the tutorial I need help understanding looks like this: ...
3
votes
1answer
65 views

WPF Extension to directly inject dependencies in the Window constructor

I just finished writing the core for my WPF application and because I'm still a very beginner at software design I wanted to get some feedback about the design decisions I've made. I've uploaded ...
3
votes
1answer
162 views

Grouping low-level services together into a high-level service

I am working on an e-commerce website. What I need to do is to save an advertisement. The ad has some properties and some photos... properties should be save to DB and photos should be saved to file ...
5
votes
2answers
164 views

Simulating a Solar System Using Command-Line Graphics

After a relative hiatus of around two years, I've decided to start delving back into the programming world; to start, I've appropriated one of my older projects, CLIGL, and have made a solar system ...