Questions tagged [c#]

C# is a programming language. Questions about programming are off topic. Please direct these questions to Stack Overflow.

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

339 questions
101
votes
12 answers

How to make CTRL + / toggle a comment in Visual Studio

How can I make CTRL + / toggle a comment in Visual Studio, as it does with XCode and Eclipse?
Marc
  • 1,113
51
votes
3 answers

How can I compile a .NET project without having Visual Studio installed?

I want to compile a .NET/C# project, but I don't want to install Visual Studio to do this. What tools do I need and how can I compile the project?
Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
30
votes
2 answers

When you start multiple threads on a multi core processor, are they guaranteed to be processed by different cores?

I have a Pentium core i5 processor, which has 4 cores. If I do this in a C# console program var t1 = new Thread(Thread1); var t2 = new Thread(Thread2); t1.Start(); t2.Start(); are t1 and t2 threads guaranteed to run on separate cores?
30
votes
5 answers

How do I add formatted code to a Microsoft Word document?

I need to write a document in MS-Word 2007 that contains a lot of examples of VB.NET and C# code. What is the best way of getting the code to look reasonable in the document? What styles etc do people use? (I do not have time to hand edit/format…
Ian Ringrose
  • 1,299
24
votes
1 answer

What is .NET Multi-Targeting Pack?

Installing Visual Studio installs a bunch of .Net packages called .NET x Targeting Pack .NET x Multi-Targeting Pack .NET x Multi-Targeting Pack (ENU) Where "x" stands for the .Net version number it supports. What are these packages for?
19
votes
8 answers

Why install for "just me" as opposed to "everybody"?

I'm using windows installer for a client's app, and they are complaining that sometimes multiple instances of an app appear on the computers they are using for testing. This problem has gone away since I hid the option install for "just me" and…
Adam B
15
votes
5 answers

How to use PSCP to copy file from Unix machine to Windows machine where target path has spaces?

I am having a problem using PSCP in a C# program to copy a file from a Unix machine to a Windows machine. The problem only happens when the target folder on the Windows machine has a space in it. For example, the following works fine: (NOTE: the…
14
votes
3 answers

How to show currently mapped drives in PowerShell?

Need a PowerShell command to display drive letter and path it is mapped to. In other words a command that shows me the same thing Windows Explorer would. Tried this: Get-WmiObject -Class Win32_MappedLogicalDisk | select Name, ProviderName and it…
Kellen Stuart
  • 618
  • 3
  • 10
  • 20
13
votes
1 answer

File does not contain a valid CIL image

I installed mono on my Suse 12.1. When I create a file, say hello.cs, and run mono hello.cs on the terminal, I get this error: Cannot open assembly 'hello.cs': File does not contain a valid CIL image. The contents of the file (hello.cs) are as…
roykasa
  • 359
9
votes
1 answer

Meaning of \b in .Net Regular Expressions

Microsoft has a nifty quick reference card for .Net Regular Expressions. But it seems to list \b as both matching Backspace and also matching "On word boundary". Which is it? Can \b really do both? How can you be precise about which one you mean?
abelenky
  • 993
9
votes
7 answers

How to launch the Windows Explorer shell after starting with a different one?

The following is a hack, but for what I need it for its fine. I created a C# program that shows some EULA text and has an Agree and Disagree button. I set the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell to launch…
Keltari
  • 75,447
8
votes
2 answers

Command line RDP call CMD on target machine

I need to remote into ~50 machines and run a batch file. Is there any way I can start up an RDP session and call cmd?
user961969
8
votes
4 answers

How often do users change their IP address (average time needed)?

Basically, i need to know how long (on average) and ip address is used per user, this can be US-only stats or World-Wide stats, if you have a source please state it. If its your own data please let us know how you've come to this determination. If…
user51047
  • 191
8
votes
2 answers

Stop Visual Studio text editor from auto moving comment lines

Is there a way to stop Visual Studio text editor (2015 or 2017) from auto moving the comment lines when pressing Ctrl+K,D or relevant key stroke (Ctrl+E,D - format entire document) ? I would like my comments to stay at the position placed,…
7
votes
1 answer

TrueCrypt drive letter not available

With c# or a batch file I mount a trueCrypt volume located at A:\volumeTrueCrypt.tc With c# I do: static void Main(string[] args) { var p = Process.Start( fileName:@"C:\Program Files\TrueCrypt\TrueCrypt.exe", arguments:@"/v…
Tono Nam
  • 889
1
2 3
22 23