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#
2125 votes
14 answers
1.5m views

AddTransient, AddScoped and AddSingleton Services Differences

I want to implement dependency injection (DI) in ASP.NET Core. So after adding this code to ConfigureServices method, both ways work. What is the difference between the services.AddTransient and ...
Elvin Mammadov's user avatar
2107 votes
114 answers
1.8m views

How do I remedy "The breakpoint will not currently be hit. No symbols have been loaded for this document." warning?

A C# desktop application (on the Visual Studio Express edition) worked, but then it didn't work 5 seconds later. I tried the following: Ensure debug configuration, debug flag, and full debug ...
2015 votes
18 answers
707k views

What do two question marks together mean in C#?

Ran across this line of code: FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); What do the two question marks mean, is it some kind of ternary operator? It's hard to look up in Google.
Edward Tanguay's user avatar
1908 votes
16 answers
1.5m views

Type Checking: typeof, GetType, or is?

I've seen many people use the following code: Type t = typeof(SomeType); if (t == typeof(int)) // Some code here But I know you could also do this: if (obj1.GetType() == typeof(int)) // Some ...
jasonh's user avatar
  • 30k
1897 votes
20 answers
435k views

Proper use of the IDisposable interface

I know from reading Microsoft documentation that the "primary" use of the IDisposable interface is to clean up unmanaged resources. To me, "unmanaged" means things like database ...
cwick's user avatar
  • 26.5k
1867 votes
26 answers
2.3m views

What is a NullReferenceException, and how do I fix it?

I have some code and when it executes, it throws a NullReferenceException, saying: Object reference not set to an instance of an object. What does this mean, and what can I do to fix this error?
1866 votes
4 answers
122k views

Is there a reason for C#'s reuse of the variable in a foreach?

When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example: foreach (var s in strings) { query = query.Where(i => i.Prop ...
StriplingWarrior's user avatar
1857 votes
10 answers
1.4m views

Calling the base constructor in C#

If I inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how do I do that? For example, if I inherit from the ...
lomaxx's user avatar
  • 115k
1764 votes
14 answers
672k views

What does the [Flags] Enum Attribute mean in C#?

From time to time I see an enum like the following: [Flags] public enum Options { None = 0, Option1 = 1, Option2 = 2, Option3 = 4, Option4 = 8 } I don't understand what ...
Brian Leahy's user avatar
  • 35.2k
1762 votes
8 answers
992k views

How to loop through all enum values in C#? [duplicate]

This question already has an answer here: How do I enumerate an enum in C#? 26 answers public enum Foos { A, B, C } Is there a way to loop through the possible values of Foos? ...
divinci's user avatar
  • 22.9k
1727 votes
29 answers
253k views

Why not inherit from List<T>?

When planning out my programs, I often start with a chain of thought like so: A football team is just a list of football players. Therefore, I should represent it with: var football_team = new ...
Superbest's user avatar
  • 26.2k
1706 votes
30 answers
520k views

What is the difference between const and readonly in C#?

What is the difference between const and readonly in C#? When would you use one over the other?
readonly's user avatar
  • 351k
1663 votes
16 answers
474k views

Why is it important to override GetHashCode when Equals method is overridden?

Given the following class public class Foo { public int FooId { get; set; } public string FooName { get; set; } public override bool Equals(object obj) { Foo fooItem = obj as ...
David Basarab's user avatar
1656 votes
42 answers
204k views

Calculate relative time in C#

Given a specific DateTime value, how do I display relative time, like: 2 hours ago 3 days ago a month ago
1649 votes
53 answers
1.2m views

How do you convert a byte array to a hexadecimal string, and vice versa?

How can you convert a byte array to a hexadecimal string and vice versa?

15 30 50 per page