Questions tagged [c#]

C# (pronounced "see sharp") is a high level, statically typed, multi-paradigm programming language developed by Microsoft. C# code usually targets Microsoft's .NET family of tools and run-times, which include .NET, .NET Framework and Xamarin among others. Use this tag for questions about code written in C# or about C#'s formal specification.

Filter by
Sorted by
Tagged with
7164votes
63answers
1.2mviews

What is the difference between String and string in C#?

Example (note the case): string s = "Hello world!"; String s = "Hello world!"; What are the guidelines for the use of each? And what are the differences?
4159votes
33answers
918kviews

How to enumerate an enum

How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { ...
user avatar
  • 232k
3634votes
32answers
1.7mviews

How can I cast int to enum?

How can an int be cast to an enum in C#?
user avatar
  • 109k
3230votes
29answers
1.1mviews

Case insensitive 'Contains(string)'

Is there a way to make the following return true? string title = "ASTRINGTOTEST"; title.Contains("string"); There doesn't seem to be an overload that allows me to set the case ...
user avatar
2998votes
30answers
2.0mviews

How to iterate over a dictionary?

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
user avatar
  • 30.2k
2776votes
13answers
386kviews

What are the correct version numbers for C#?

What are the correct version numbers for C#? What came out when? Why can't I find any answers about C# 3.5? This question is primarily to aid those who are searching for an answer using an incorrect ...
2480votes
54answers
922kviews

Deep cloning objects

I want to do something like: MyObject myObj = GetMyObj(); // Create and fill a new object MyObject newObj = myObj.Clone(); And then make changes to the new object that are not reflected in the ...
user avatar
  • 47.1k
2419votes
29answers
718kviews

Catch multiple exceptions at once?

It is discouraged to simply catch System.Exception. Instead, only the "known" exceptions should be caught. Now, this sometimes leads to unnecessary repetitive code, for example: try { ...
user avatar
  • 172k
2313votes
40answers
1.2mviews

How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

How do I convert a string to a byte[] in .NET (C#) without manually specifying a specific encoding? I'm going to encrypt the string. I can encrypt it without converting, but I'd still like to know ...
user avatar
  • 55.6k
2286votes
12answers
247kviews

Should 'using' directives be inside or outside the namespace?

I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace. Is there a technical reason for putting the using directives inside ...
user avatar
  • 36.2k
2244votes
32answers
2.7mviews

How do I generate a random int number?

How do I generate a random integer in C#?
user avatar
  • 61.7k
2185votes
23answers
1.1mviews

What is the best way to give a C# auto-property an initial value?

How do you give a C# auto-property an initial value? I either use the constructor, or revert to the old syntax. Using the Constructor: class Person { public Person() { Name = "...
user avatar
  • 31.7k
2150votes
29answers
2.0mviews

Get int value from enum in C#

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this. public enum Question { Role = 2, ProjectFunding = 3, ...
user avatar
  • 24.9k
2104votes
70answers
730kviews

How do I calculate someone's age based on a DateTime type birthday?

Given a DateTime representing a person's birthday, How do I calculate their age in years?
2076votes
46answers
1.2mviews

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

How can I create an Excel spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?

15 30 50 per page
1
2 3 4 5
101933