Questions tagged [c#]

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

C# is a multiparadigm, managed, garbage-collected object-oriented programming language created by Microsoft in conjunction with the .NET platform, but also used with non-Microsoft implementations (most notably, Mono).

Versions 1.0/1.2 and 2.0 of C# were submitted and approved as both ECMA and ISO/IEC standards. As of December 2010, there are no ECMA or ISO/IEC specifications for C# 3.0 and 4.0, however language specifications are available from Microsoft (3.0 and 4.0 respectively).

The language's type-system was originally static, with only explicit variable declarations allowed. However, the introduction of var (C# 3.0) and dynamic (C# 4.0) allow it to use type-inference for implicit variable typing, and to consume dynamic type-systems, respectively. Delegates (especially with lexical-closure support for anonymous-methods (C# 2.0) and lambda-expressions (C# 3.0)) allow the language to be used for functional programming.

Compilation is usually to the Common Intermediate Language (CIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR); however, options like Ngen (.NET) and AOT (Mono) mean this isn't the only option. Additionally, some frameworks (e.g. the Micro Framework) act as CIL interpreters, with no JIT.

Perhaps unusually, generics in C# are provided (in part) by the runtime, unlike (for comparison) C++ templates, or Java's generics (which use type-erasure).

With the combination of Microsoft .NET for Windows (desktop/server), Mono (desktop/server/mobile), Silverlight / Moonlight (browser/mobile), Compact Framework (mobile), and Micro Framework (embedded devices), it is available for a wide range of platforms.

Hello World

using System;
class Hello
{
    static void Main() 
    {
        Console.WriteLine("Hello, World");
    }
}

FAQs

Resources

4886 questions
305
votes
19 answers

Are #regions an antipattern or code smell?

C# allows the use of #region/#endregion keywords to make areas of code collapsible in the editor. Whenever I do this though I do it to hide large chunks of code that could probably be refactored into other classes or methods. For example I have seen…
Craig
  • 4,302
  • 3
  • 21
  • 21
252
votes
11 answers

Why do many exception messages not contain useful details?

It seems there is a certain amount of agreement that exception messages should contain useful details. Why is it that many common exceptions from system components do not contain useful details? A few examples: .NET List index access…
Martin Ba
  • 7,578
  • 7
  • 34
  • 56
183
votes
5 answers

When do you use a struct instead of a class?

What are your rules of thumb for when to use structs vs. classes? I'm thinking of the C# definition of those terms but if your language has similar concepts I'd like to hear your opinion as well. I tend to use classes for almost everything, and use…
RationalGeek
  • 10,077
  • 7
  • 38
  • 56
169
votes
3 answers

Benefits of Structured Logging vs basic logging

We're building a new app and I'd like to include structured logging. My ideal setup would be something like Serilog for our C# code, and Bunyan for our JS. These would feed into fluentd and then could go out to any number of things, I was thinking…
DTI-Matt
  • 1,799
  • 2
  • 10
  • 5
159
votes
6 answers

SOLID Principles and code structure

At a recent job interview, I couldn't answer a question about SOLID -- beyond providing the basic meaning of the various principles. It really bugs me. I have done a couple of days worth of digging around and have yet to come up with a satisfactory…
S-Unit
  • 1,397
  • 4
  • 10
  • 9
157
votes
8 answers

How do you organize your projects?

Do you have any particular style of organizing projects? For example, currently I'm creating a project for a couple of schools here in Bolivia, this is how I organized it: TutoMentor (Solution) TutoMentor.UI (Winforms project) TutoMentor.Data…
Sergio
149
votes
7 answers

Relationship between C#, .NET, ASP, ASP.NET etc

I'm really unclear on the difference between C#, C#.NET and the same for ASP and other '.NET' languages. From what I understand, .NET is a library/framework of... things. I think they're essentially access to Windows data such as form elements etc,…
Megan Walker
  • 2,016
  • 2
  • 14
  • 11
140
votes
4 answers

Creating database connections - Do it once or for each query?

At the moment I create a database connection when my web page is first loaded. I then process the page and run any queries against that conection. Is this the best way to do it or should I be creating a database connection each time I run a…
webnoob
  • 2,139
  • 3
  • 19
  • 20
127
votes
10 answers

Don't Use "Static" in C#?

I submitted an application I wrote to some other architects for code review. One of them almost immediately wrote me back and said "Don't use static. You can't write automated tests with static classes and methods. static is to be avoided." I…
Infin8Loop
  • 1,459
  • 2
  • 11
  • 16
118
votes
16 answers

Should I add redundant code now just in case it may be needed in the future?

Rightly or wrongly, I'm currently of the belief that I should always try to make my code as robust as possible, even if this means adding in redundant code / checks that I know won't be of any use right now, but they might be x amount of years down…
108
votes
5 answers

Aren't the guidelines of async/await usage in C# contradicting the concepts of good architecture and abstraction layering?

This question concerns the C# language, but I expect it to cover other languages such as Java or TypeScript. Microsoft recommends best practices on using asynchronous calls in .NET. Among these recommendations, let's pick two: change the signature…
corentinaltepe
  • 971
  • 2
  • 6
  • 6
103
votes
14 answers

At what point is brevity no longer a virtue?

A recent bug fix required me to go over code written by other team members, where I found this (it's C#): return (decimal)CostIn > 0 && CostOut > 0 ? (((decimal)CostOut - (decimal)CostIn) / (decimal)CostOut) * 100 : 0; Now, allowing there's a good…
Bob Tway
  • 3,606
  • 3
  • 21
  • 26
101
votes
6 answers

Should we create a new single instance of HttpClient for all requests?

recently I came across this blog post from asp.net monsters which talks about issues with using HttpClientin following way: using(var client = new HttpClient()) { } As per the blog post, if we dispose the HttpClient after every request it can keep…
Ankit Vijay
  • 1,568
  • 3
  • 10
  • 13
99
votes
14 answers

Is it considered an anti pattern to write SQL in the source code?

Is it considered an anti pattern to hardcode SQL into an application like this: public List getPersonIDs() { List listPersonIDs = new List(); using (SqlConnection connection = new SqlConnection( …
w0051977
  • 7,031
  • 6
  • 59
  • 87
98
votes
4 answers

Rich Domain Models -- how, exactly, does behavior fit in?

In the debate of Rich vs. Anemic domain models, the internet is full of philosophical advice but short on authoritative examples. The objective of this question is to find definitive guidelines and concrete examples of proper Domain-Driven Design…
RJB
  • 2,090
  • 1
  • 14
  • 11
1
2 3
99 100