Skip to main content

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 ecosystem, which include .NET, .NET Framework, .NET MAUI, and Xamarin among others. Use this tag for questions about code written in C# or about C#'s formal specification.

c#
7550 votes
68 answers
1.3m views

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

What are the differences between these two, and which one should I use? string s = "Hello world!"; String s = "Hello world!";
4393 votes
36 answers
1.0m views

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() { ...
Ian Boyd's user avatar
  • 253k
3909 votes
32 answers
2.1m views

How do I cast int to enum in C#?

How do I cast an int to an enum in C#?
lomaxx's user avatar
  • 115k
3447 votes
32 answers
1.3m views

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 ...
Boris Callens's user avatar
3312 votes
20 answers
2.4m views

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?
Jake Stewart's user avatar
  • 33.3k
2900 votes
13 answers
430k views

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 ...
2646 votes
59 answers
1.0m views

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 ...
NakedBrunch's user avatar
  • 49.1k
2583 votes
29 answers
858k views

Catch multiple exceptions at once?

It is discouraged to catch System.Exception errors. Instead, only the "known" exceptions should be caught. This sometimes leads to unnecessary repetitive code, for example: try { WebId = ...
Michael Stum's user avatar
2432 votes
32 answers
3.1m views

How do I generate a random integer in C#?

How do I generate a random integer in C#?
Rella's user avatar
  • 66.3k
2413 votes
15 answers
283k views

Should 'using' directives be inside or outside the namespace in C#?

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 ...
benPearce's user avatar
  • 38.1k
2367 votes
41 answers
1.2m views

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 ...
Agnel Kurian's user avatar
  • 58.9k
2365 votes
32 answers
2.4m views

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, ...
jim's user avatar
  • 27.1k
2329 votes
23 answers
1.3m views

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 = "...
bentford's user avatar
  • 33.3k
2254 votes
75 answers
835k views

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?
2173 votes
48 answers
1.4m views

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
107969