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

4931 questions
42
votes
14 answers

Is this a correct situation to use a constant?

So my professor was giving back some feedback on a project I've been working on. He docked a few marks for this code: if (comboVendor.SelectedIndex == 0) { createVendor cv = new createVendor(); cv.ShowDialog(); loadVendors(); } This is in a…
Red 5
  • 527
41
votes
4 answers

Is having public constants "bad"?

Is this: public MyClass { public const string SomeString = "SomeValue"; } worse than this: public MyClass { public static string SomeString { get{ return "SomeValue";}} } Both can be referenced the same way: if (someString ==…
Vaccano
  • 4,048
36
votes
4 answers

What is the point of the string.Empty property

Why was the property string foo = string.Empty included in the BCL? It seems more verbose and no clearer than just using an empty string (string foo = "")
Tom Squires
  • 17,755
  • 11
  • 67
  • 88
27
votes
2 answers

When is it appropriate to use expression bodied members?

C# 6 introduces expression-bodied members, which allow for simplified code in methods and properties that only return the result of an expression: public override string ToString() { return string.Format("{0} {1}", _field1,…
Asik
  • 864
27
votes
5 answers

Which are the cases when 'uint' and 'short' datatypes are a better fit than the standard int(32)?

I understand the differences in capacity and values they can represent but it seems as though people always use Int32 regardless of whether it is appropriate. No one ever seems to use the unsigned version (uint) even though a lot of the time it fits…
Alternatex
  • 1,033
26
votes
20 answers

Is learning C# as a first language a mistake?

I know there are similar questions on here, which I've read, but I recently read this post by Joel Spolsky: How can I teach a bright person, with no programming experience, how to program? And it got me thinking about my way of learning and whether…
23
votes
2 answers

What is generator code?

Reading the Google C# style guide I came across this: Generators vs containers Use your best judgement, bearing in mind: Generator code is often less readable than filling in a container. Generator code can be more performant if the results are…
22
votes
3 answers

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 within the algorithm. After the algorithm is…
21
votes
6 answers

Should Properties have side effects

Should properties in C# have side effects beside notifying of a change in it's states? I have seen properties used in several different ways. From properties that will load the value the first time they are accessed to properties that have massive…
Erin
  • 2,358
  • 3
  • 19
  • 23
21
votes
6 answers

"Undoing" an integer wraparound

I ran into an interesting theoretical problem a number of years ago. I never found a solution, and it continues to haunt me when I sleep. Suppose you have a (C#) application that holds some number in an int, called x. (The value of x is not fixed).…
Xcelled
  • 331
21
votes
2 answers

creating object parameters in {}?

I am trying to decode a piece of code from a book: List people = new List() { new Person {FirstName="Homer",LastName="Simpson",Age=47}, new Person {FirstName="Marge",LastName="Simpson",Age=45} }; Person is just a simple class they…
20
votes
4 answers

What is the most elegant way to write a "Try" method in C# 7?

I am writing a type of Queue implementation that has a TryDequeue method that uses a pattern similar to various .NET TryParse methods, where I return a boolean value if the action succeeded, and use an out parameter to return the actual dequeued…
Eric Sondergard
  • 311
  • 2
  • 7
20
votes
3 answers

Should I use .ToString() when concatenating string and integer variables in C#?

int a = 1; int b = 2; int sum = a + b; string expression = "Expression: " + a + " + " + b + " = " + sum; Console.WriteLine(expression); //displays Expression 1 + 2 = 3 Should I use: string expression = "Expression: " + a + " + " + b + " = " +…
Akainu
  • 343
17
votes
9 answers

Should if statments be in inner or outer method?

Which of these designs is better? What are the pros and cons of each? Which one would you use? Any other suggestions of how to deal with methods like is are appreciated. It is reasonable to assume that Draw() is the only place that the other draw…
mjcopple
  • 323
17
votes
5 answers

Storing data in code

A few times in my past I've wanted to store data in code. This would be data that seldom changes and is used in places where access to a database isn't possible, practical, or desirable. A small example would be storing a list of countries. For that…
mroach
  • 305
1
2 3
13 14