C# is a multiparadigm, managed, garbage-collected object-oriented programming language created by Microsoft in parallel with the .NET platform

learn more… | top users | synonyms

7
votes
4answers
1k views

Is it bad practice to make methods public solely for the sake of unit testing? [duplicate]

I have a class with a public method. It has other methods that 'assist' with the purpose of the public method. The public method must be tested. However, I also want to unit test the private methods. ...
0
votes
2answers
96 views

Library design: better to leave potentially destructive feature in with a warning, or cut it out completely?

I recently created a red-black tree in C# to better understand how it works. With it, I implemented an in-order enumerator, however I quickly realized that enumerating a tree can have destructive ...
4
votes
4answers
260 views

Styleguide when coding in a static language [duplicate]

I am currently a junior engineer and keen on learning best practices and expanding my experience. My question concerns any static programming language such as Java, C#, C++ etc. When I am writing ...
0
votes
1answer
61 views

Lock object in base class: private or protected? [closed]

I have a base class that contains some lists, queues, and other fields which determine its state. In the base class I only access these fields within a protected lock object since there could be ...
1
vote
1answer
110 views

Using and referencing external executables in a solution

I have an application written in AS3 which I need to be able to start up from my C# application. I have a prototype that does this just fine but my question is in relation to how I would arrange my ...
1
vote
4answers
242 views

How to make one test depend on the results of another test?

Let's say there is a utility class that provides some common static methods used everywhere in your code by many other classes. How would you design your unit tests for the consumers of the utility ...
34
votes
4answers
3k views

Why structs and classes are separate concepts in C#?

While programming in C#, I stumbled upon a strange language design decision that I just can't understand. So, C# (and the CLR) has two aggregate data types: struct (value-type, stored on the stack, ...
2
votes
1answer
207 views

Reducing the complexity of over-designed code

I have just started working at a company where I have inherited a C# codebase from a previous developer. I know programming well, have an engineering degree + an (unfinished, several year long) PhD ...
0
votes
0answers
47 views

Result with value or error returned from service c# [duplicate]

First of all, let me explain the current situation. In a ASP.NET project, we do some ajax calls to a WCF service. This service always returns a value when called. But this is the way they implemented ...
0
votes
2answers
111 views

Create the fields in class level then instantiate inside methods or create and instantiated inside methods

I'm a newbie in software development. Just wondering which code is better and why should I continue which pattern should I follow. First Snippet: Class TestClass { private Object1 field = null; ...
2
votes
3answers
177 views

Concurrent collections, should read methods allow multiple threads at one time?

I have custom collection and I want to add wrapper to allow concurrent access. public class MyConcurrentCollection<T> { private MyCollection _collection; // passed in constructor ...
0
votes
3answers
168 views

Write a (good) code convention for c# [closed]

As a relative new trainee in my company, I've been asked to write a code convention since the project is 6 years old and nothing have been done. Here's the points I came up with: VARIABLE ...
3
votes
1answer
226 views

Tracing an object

Suppose I want an object with a list of strings and every method that this object gets passed to adds its name to the list of strings. Basically it's a stack trace, except unlike a stack trace it ...
1
vote
2answers
92 views

Technique to synchronize error codes in a same-project-API

In the project I am working right now, we have some python and some C# code. At some point, I call from python a subprocess which starts a C# executable. This C# code returns an error code, which has ...
2
votes
5answers
270 views

Is it bad or good to wrap mutable objects in immutable containers?

If I have parts of code that need to mutate the underlying data structure but then others that consume, not mutate the structure, is it good practice to create a wrapper which does not expose mutation ...
1
vote
1answer
133 views

Does merely parameterizing a dependency meet the requirements for Inversion of Control?

When I find a concrete dependency inside an extension method, I have been attempting to remove the (concrete) dependency by parameterising it like so // original implementation public static ...
0
votes
1answer
43 views

Separate Drawable objects and their Data

I am looking for a way to design my application. Suppose you had a car drawing thing. Car has multiple parts.Some are going to be visible some are not.For example battery should be drawn. So someone ...
1
vote
0answers
114 views

How shall I project CQRS command and query services through WCF

I got directed to this site for more conceptual questions. Here is my issue: Im designing an application that uses CQRS to expose functionality to higher levels in the stack. This looks like below ...
4
votes
2answers
223 views

Is checking that a property doesn't throw an exception a valid unit test?

I'm writing tests for the following class: public class Foo : INotifyPropertyChanged { private int _failCount; private int _totalCount; public double FailRate { get { double ...
-1
votes
4answers
267 views

Is static going to make any difference in this code snippet

I have been to an interview and was asked this question - is there any difference adding or removing the static keyword in these classes? I know what static means but my understanding of this point ...
1
vote
2answers
159 views

Smells in exception handling: how to separate between public and internal exceptions?

During the development of a small web API, we decided to separate internal exceptions from public exceptions. Public exceptions are HTTP exceptions, that translate into HTTP responses with proper ...
1
vote
3answers
121 views

Accessing shared data without blocking in TPL

I am writing a class that contains data. It exposes methods that allow to query the data, while the data is also being updated from an external source (web service, for example). All the methods ...
0
votes
1answer
118 views

Writing code with language features yet to come in mind

In C#6, we will (most likely) have the nameof operator, a very useful operator for the ArgumentNullException pattern: public void M(T x) { if (x == null) { throw new ...
1
vote
2answers
165 views

Which design (pattern) would be suitable to abstract logging platforms?

I need to create a simple system to abstract logging platforms. The idea is that I'd like to be able to exchange or activate a logging platform (nlog, log4net, enterprise library) via some ...
0
votes
1answer
195 views

Is it good practice to shorten functions? [duplicate]

Is it good practice to shorten functions? Like this code for instance: using System; namespace Hello { class Program { public static void ...
3
votes
1answer
148 views

How can I use the “Non-Member Functions Improve Encapsulation” pattern from C#?

In 2000, Scott Meyers argued that non-member functions improve encapsulation. In C++, a non-member function is a C-style global function: ...
0
votes
3answers
235 views

How to save complex data to file/disk?

I am working on a side project for a small business owner (my father) and part of the requirements for the application include saving customer and job related data in a way that it can easily be ...
1
vote
1answer
100 views

Software architecture confusion App-Server/Client

i have a question about the following software architecture: So, there is one database, an app server, which provide a restful service, a client and a framework, used by the app server and the ...
-1
votes
1answer
104 views

Would it be troublesome to learn C# and F# at the same time? [duplicate]

I was pondering the idea of learning F# while I learned C#. It isn't my first programming language. I learned Python first and I saw F# and it seemed very much the same. I thought it may be hard to ...
0
votes
1answer
89 views

Indexers in C# for single objects

I was wondering whether it was possible to also use indexers for single objects. So, class A<T> { public T this[int index] { //get, set using index } private T _t; } I was wondering ...
3
votes
1answer
155 views

How to design similar purpose static methods

I know that you cannot override static functions, but for what I'm looking to do, it seems like the logical way to design my class. I have a method that is associated with my abstract class, that i ...
13
votes
3answers
633 views

What is the difference between ref and out in runtime?

C# provides the ref and the out keyword to make arguments to be passed by reference. The semantic of the two is very similar. The only difference is in the initialization of the flaged variable: ref ...
3
votes
2answers
345 views

Best practice to include XSD in C# project

I have an XSD schema I need to include within my program, to validate some user input XML. The schema will need to be regularly updated. (By the programmer.) My current method to include this is hold ...
7
votes
2answers
372 views

Why is the 'out' keyword used in two seemingly disparate contexts?

In C#, the out keyword can be used in two different ways. As a parameter modifier in which an argument is passed by reference class OutExample { static void Method(out int i) { i = ...
1
vote
3answers
270 views

C# if statements

I am currently working on a C# checkers game for a university project in programming. But i have came to the point where i need to check whether a piece can be moved, or jump, but i feel like i'm ...
17
votes
7answers
3k views

Why is instance creation the way it is?

I've learned C# over the course of the past six months or so and am now delving into Java. My question is about instance creation (in either language, really) and it's more of: I wonder why they did ...
0
votes
3answers
215 views

Benefits of using .NET style delegates compared to custom ones

I know about C# events and delegates. I find them incredibly useful for event-driven sub-systems. One thing I don't understand, however, is why all the .NET documentation for events uses a very ...
2
votes
1answer
246 views

Avoiding closures

I have a data structure in the form of a tree. Each node represents a cardboard box. The boxes can contain child boxes. public class CardboardBox { public int Id {get; set;} public int ...
22
votes
3answers
1k views

Caching at business layer vs Caching at Data Layer

I have always worked on projects where caching was done on DAL, basically just when you are about to make the call to database, it checks if data is already there in the cache and if it is, it just ...
0
votes
2answers
109 views

Should methods perform checks that they accomplished the task they were built for? or should they just throw an exception?

What are some pro's and con's of validating your performed the task intended? public static bool UploadFile(string filename) { // 1. upload the file // 2. check to see if the file now exist ...
0
votes
1answer
69 views

Repository strategy for multiple interconnected components with small differences

At my work we have multiple products.. productA, productB, ... these products reference multiple components. componentA, componentB.. there are also shared components sharedA, sharedB... etc.. which ...
0
votes
1answer
102 views

Dynamic model based on JSON received from web service

I am creating a MVC web application which receives some data from a web service in JSON format. I get the different content type structures and data items related to that structure. Using that JSON I ...
0
votes
3answers
174 views

Convention for logging within nested try catch blocks

I've looked at this question but it doesn't really help solve my dilemma. System 1 calls system 2 within a try catch, the method in system 2 has it's own try catch, which sadly handles an exception ...
1
vote
0answers
308 views

Should UserControl's be Views in an XAML MVVM application (WPF or Store app)?

I am about to use Caliburn.Micro for a new XAML Store app (not sure what to call it nowadays - does not appear to be called a "Windows Store app" any longer). The documentation (see Simple Navigation ...
1
vote
0answers
128 views

Unsafe redundancy refactoring in ReSharper 9.0? [closed]

Considering the following scenario: public class A { ... public virtual void Foo() { ... } ... } public class B : A { public void DoSomething() { ... base.Foo(); ... ...
3
votes
2answers
207 views

Software development - The industry & general trends / Bad practices [duplicate]

I am a Web Developer and part of a small team working on an abundance of projects. This is my first "actual" real company after graduating with a degree in computer Science and I have about 2 years ...
-1
votes
2answers
283 views

Would redrawing controls in c# give more performance?

Now I realize this is a kind of weird question but here it is: Let us assume I redraw all the controls by myself rather than using the default provided ones (Obviously I'm talking of winforms as WPF ...
1
vote
2answers
173 views

Why can we use the same name for local variable in different scopes?

I was wondering why is it possible that we can use the same name for local variable in different scopes? Like we can have two methods which both can have variable with the same name. What makes ...
1
vote
3answers
207 views

Avoiding a large amount of overloads

I'm currently writing a mailing interface for our enterprise application. Dependent on the send mode (regular email, bulk email, templates, mailmerge, ...) our Send() method requires a bunch of ...
1
vote
0answers
41 views

Best style to use multiple custom UserControls in a Grid

I have the following: A MainWindow; with Children on the MainGrid in the MainWindow: ContentControl, displays the main content of the program and has a taskbar at the bottom for user interaction ...