Questions tagged [c#]

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

3
votes
5answers
12k views

Interview Question - Adding Method to interface that has been implemented by thousands of class [closed]

There is scenario where I have Interface X, which has been implemented with my thousands of classes. Now I want to add new method in that Interface X. So how to make the changes in minimal way to ...
1
vote
2answers
5k views

Anemic Domain Model vs. DDD by definition example(s)?

What is an example of "Business Logic" that should reside in the DomainModel i.e. inside an Entity instead of inside a (Domain) Service, as well as some example logic that should be in a service. ...
26
votes
9answers
3k views

Why should one want to disable compiler warnings?

This answer and the comments added to it show a way to disable several compiler warnings using #pragma directives. Why would one want to do that? Usually the warnings are there for a reason, and I've ...
16
votes
4answers
664 views

What are the drawbacks to mapping integral identifiers to enums?

I've been thinking about creating custom types for identifiers like this: public enum CustomerId : int { /* intentionally empty */ } public enum OrderId : int { } public enum ProductId : int { } My ...
16
votes
4answers
2k views

Why do we need the async keyword?

I just started playing around with async/await in .Net 4.5. One thing I'm initially curious about, why is the async keyword necessary? The explanation I read was that it is a marker so the compiler ...
14
votes
3answers
5k views

Can someone explain to me C#'s coding convention?

I recently started working with Unity3D and primarily scripting with C#. As I normally program in Java, the differences aren't too great but I still referred to a crash course just to make sure I am ...
12
votes
5answers
2k views

Why have minimal user/handwritten code and do everything in XAML?

I feel the MVVM community has become overzealous like the OO programmers in the 90's - it is a misnomer MVVM is synonymous with no code. From my closed StackOverflow question: Many times I come ...
11
votes
9answers
529 views

Studies on code documentation productivity gains/losses

After much searching, I have failed to answer a basic question pertaining to an assumed known in the software development world: WHAT IS KNOWN: Enforcing a strict policy on adequate code ...
9
votes
4answers
10k views

Separation of retrieving data and business objects between DAL and BLL Layers

I did some research before posting this question. Among other questions or post, one of those is provided below. I could not get a clear mind how to determine.. Business Objects within a Data Access ...
8
votes
1answer
1k views

In a multi tier project where should interfaces be defined?

I have a multi tiered project made up of three sub projects, i.e. Data access project, Business Logic project and Presentation project, where should interfaces be defined? I'm guessing that there ...
7
votes
1answer
4k views

Repository Pattern and database queries

Having read others posts, it already seems to me that the concept "repository" and "database" do not go well hand in hand, as they are meant to be completely separate concepts.... but I'll ask the ...
7
votes
5answers
602 views

How do you define a responsbility?

I learnt the SOLID principles back in 2011 and I try to use them in my day to day work. However, I often find myself wandering if I am breaking the Single Responsibility principle. A class can have ...
7
votes
1answer
1k views

How can we calculate Big-O complexity in Functional & Reactive Programming

I started learning functional programming, I am trying to compare between different algorithms that are written in an imperative, functional , parallel programming and using Collections and Lambda ...
7
votes
6answers
19k views

Is this a Best Practice with Enum in C#

When Enum is used as below, say if we have enum Designation { Manager = 0, TeamLead = 1, Associate = 2 } then write the below code if (designation == Designation.TeamLead) //somecode ...
6
votes
5answers
6k views

Using a SetProperty method to prevent accidental changes to a property

Is it good/bad practice to do the following: public class MyClass { public MyType MyProperty { get; private set; } public void SetMyProperty(MyType myProperty) { MyProperty = myProperty; } ...
6
votes
3answers
1k views

How do I support per-method-call instantiation without referencing the IoC container outside the composition root?

I read with interest this article which states: A DI Container should only be referenced from the Composition Root. All other modules should have no reference to the container. This means that ...
5
votes
2answers
1k views

Should my classes have separate constructors just for unit testing?

I like to write classes with two constructors: a primary constructor used in production code, and a default constructor just for unit tests. I do this because the the primary constructor creates other ...
5
votes
5answers
14k views

What problems can be solved using Generics?

I haven't used Generics in C# for a long while. Every time I think I need to use them I either go in the wrong direction and give up or find that I don't really need them. I feel that I'm missing out ...
5
votes
6answers
2k views

What are ways I can speed of development time when building applications? [closed]

I noticed that overtime with experience that the curve of learning shifts from trying to learn a language or technology (the way it works) to how to develop applications faster and with less code. I ...
4
votes
4answers
2k views

Is function memoization really only for primitives?

I was thinking about this for quite some time. Is function memoization really only for primitives? I currently have this piece of code: Public Shared Function Mize(Of TArg1 As Structure, TResult)(...
4
votes
7answers
8k views

Which is better: many class definitions in the same file or every class definition in a separate file?

Which is better: many class definitions in same file or every class definition in separate file? Examples: 1) Many classes in same file. Say, myManyClasses.cs: namespace myPack { class myClass1(...
4
votes
3answers
197 views

How do I model Infinite-Depth Objects?

I'm working on a web application that manages specimen freezers so our researchers can manage and inventory all the gross stuff they've made. A freezer looks like this: 1 Freezer +- X Racks +- Y ...
3
votes
1answer
1k views

Doubts about architectural pattern for customizable standard enterprise application [duplicate]

My company needs to build an application to be rolled out for a lot of clients. This software will have many modules and functionalities, but some of these will need specific adjustments for some ...
3
votes
4answers
4k views

Should I be learning Linq, Direct SQL Commands (in .net), EF or other? [closed]

Basically, I have a very good knowledge of plain old SQL coming from Classic ASP programming. Over the past couple of months, I have been learning C# and today was my first full day at MVC 3 (Razor) ...
3
votes
1answer
782 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 ...
2
votes
5answers
2k views

How to handle or prevent all Admins and users being removed

I have a website that stores all users in a database. A user can log on to the website and if they have sufficient privileges can add/delete users, as well as change their privileges. How do I ensure ...
2
votes
3answers
2k views

How do I know if I'm violating MVVM with WPF? [closed]

I was recently grabbing an auto generated TreeViewItem from a nested TreeView node in WPF, and I was using ItemContainerGenerator to get it in code behind. Then I thought to myself I'm certainly ...
2
votes
1answer
3k views

Polling versus push notifications

I have a hybrid web app for android. The webpage or website the app shows is in asp.net. The javascript for the webpage running in the webview of the app is sending out an ajax request every 10 ...
2
votes
2answers
678 views

C# Design Issue [closed]

I am building a small application and I am trying to understand the best way to approach the design. I am looking for some guidance/advice how best to approach the following issue. What I have is ...
2
votes
4answers
1k views

Should the Data Model be identical to the domain model for mapping purposes?

Say I have a data model, which looks like this: public class DataCustomer { public virtual System.DateTime CreatedTime { get; set; } public virtual Guid Id { get; set; } public virtual ...
23
votes
8answers
5k views

Is it common to use partial classes to achieve 'modularity'?

I recently encountered a situation in our codebase where a different team created a 'god class' containing around 800 methods, split across 135 files as a partial class. I asked the other team about ...
22
votes
3answers
3k views

Class with members that are mutable during creation but immutable afterwards

I have an algorithm which creates a collection of objects. These objects are mutable during creation, since they start out with very little, but then they are populated with data in different places ...
13
votes
8answers
22k views

Practical way to store a “reasonably large” amount of data that hardly ever changes?

Think in terms of pre-computed lookup tables or something. At what point does it make more sense to use a database instead of hardcoding values in my application? The values are not going to change, ...
11
votes
6answers
2k views

What are the drawbacks of immutable types?

I see myself using more and more immutable types when the instances of the class are not expected to be changed. It requires more work (see example below), but makes it easier to use the types in a ...
10
votes
3answers
9k views

Do private static methods in C# hurt anything?

I created a private validation method for a certain validation that happens multiple times in my class (I can't store the validated data for various reasons). Now, ReSharper suggests that the function ...
10
votes
9answers
3k views

Why do variables need a type?

So we write: Customer c = new Customer(); Why is the design not such that we write: c = new Customer(); c.CreditLimit = 1000; The compiler can work out c points to a Customer and allow Customer's ...
9
votes
5answers
2k views

What is the standard for modeling modern applications prior to development?

I'm taking on my first enterprise-level application and I want my team to model the entire ASP.NET MVC C# application before we even tap out one line of code. UPDATE: This was not intended to be a ...
9
votes
7answers
1k views

Should I be bothered if my LOC/day ratio is too high? [closed]

I'm currently working on an indie project, so I don't exactly have the luxury of throughout human testing or external code review — however, I don't see any difficult bugs in my current code (I fix ...
8
votes
4answers
605 views

Dependency Inversion expands the API, results in unnecessary tests

This question has bothered me for a few days, and it feels like several practices contradict each other. Example Iteration 1 public class FooDao : IFooDao { private IFooConnection fooConnection;...
7
votes
4answers
705 views

Can I make my constructors less repetitive?

I'm extending a class with 10 different constructors. The new subclass, SpecialImage, is used like this: SpecialImage specialImage = new SpecialImage(..); // Leverage the Rotate() method of ...
7
votes
2answers
8k views

How to structure an Onion project

Here is an example implementation using Onion architecture: https://www.codeproject.com/Articles/1028481/Understanding-Onion-Architecture The webpage suggests a solution structure of: Domain - ...
6
votes
7answers
2k views

What speaks against using structs for passive data structures?

Context I recently read about the object-oriented technique of making a distinction between objects and passive data structures, best summarized in Clean Code: "Objects hide their data [...] and ...
6
votes
15answers
3k views

Why OOP is enforced in Java and C#? [closed]

In many other languages like C++ and Javascript, OOP is optional. Procedural code is ok. But in languages like Java and C#, OOP is somewhat enforced. Everything is to be a part of a class or an object....
6
votes
2answers
2k views

Is server validation necessary with client-side validators?

I recently created a .net web app that used over 200 custom validators on one page. I wrote code for both ClientValidationFunction and OnServerValidate which results in a ton of repetitive code. My ...
5
votes
2answers
19k views

In ASP.NET MVC/Razor, how to add initializer JavaScript to a “control”?

Actually, I already have at least 3 different solutions for the problem, I just don't like any of them for various reasons. In ASP.NET MVC/Razor, there are no controls anymore, in the sense as they ...
5
votes
3answers
550 views

C# Dynamic types

I just read a chapter in a programming book about Dynamic types. Although they are quite neat I cant think of a single real world example where I would use them. Does anyone here actually use them and ...
5
votes
2answers
2k views

How do you apply Single Responsibility principle to a repository

I am trying to apply "SOLID" whenever I can and try to use common sense and avoid a pattern when I see that a pattern is creating more problems than it's trying to solve. I don't want to apply a ...
5
votes
4answers
800 views

Legitimate cases of having .equals() behaving inconsistently with .compareTo()?

Java documentation says it's "strongly recommended" to have them behaving consistently. But are there legitimate cases of java/c#/python/etc Object.equals() method behaving inconsistently with the ...
5
votes
3answers
5k views

When are chained assignments (i.e. a=b=c) bad form?

I'm working on a VB.Net WinForms project and found myself writing code like this: this.Fizz.Enabled = this.Buzz.Enabled = someCondition; I couldn't decide whether that was bad code or not. Are there ...
4
votes
1answer
1k views

Drawback of implementing DDD service as static class?

Can Services in a domain-driven design be implemented as C# static class? What are the drawbacks of this choice? Can it be implemented as a non-singleton non-static class?