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.
1,620,105
questions
1646
votes
24
answers
1.8m
views
How to Sort a List<T> by a property in the object
I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a list of this Order class:
List<Order> objListOrder = new List<Order>();
...
1638
votes
31
answers
367k
views
When should I use a struct rather than a class in C#?
When should you use struct and not class in C#? My conceptual model is that structs are used in times when the item is merely a collection of value types. A way to logically hold them all together ...
1636
votes
6
answers
121k
views
Try-catch speeding up my code?
I wrote some code for testing the impact of try-catch, but seeing some surprising results.
static void Main(string[] args)
{
Thread.CurrentThread.Priority = ThreadPriority.Highest;
Process....
1603
votes
22
answers
489k
views
'Static readonly' vs. 'const'
I've read around about const and static readonly fields. We have some classes which contain only constant values. They are used for various things around in our system. So I am wondering if my ...
1597
votes
47
answers
843k
views
How do I update the GUI from another thread?
Which is the simplest way to update a Label from another Thread?
I have a Form running on thread1, and from that I'm starting another thread (thread2).
While thread2 is processing some files I would ...
1570
votes
19
answers
611k
views
Why is Dictionary preferred over Hashtable in C#?
In most programming languages, dictionaries are preferred over hashtables.
What are the reasons behind that?
1566
votes
33
answers
722k
views
What is the difference between a field and a property?
In C#, what makes a field different from a property, and when should a field be used instead of a property?
1522
votes
41
answers
499k
views
Path.Combine for URLs?
Path.Combine is handy, but is there a similar function in the .NET framework for URLs?
I'm looking for syntax like this:
Url.Combine("http://MyUrl.com/", "/Images/Image.jpg")
which would return:
"...
1494
votes
18
answers
227k
views
Virtual member call in a constructor
I'm getting a warning from ReSharper about a call to a virtual member from my objects constructor.
Why would this be something not to do?
1491
votes
23
answers
1.3m
views
LINQ's Distinct() on a particular property
I am playing with LINQ to learn about it, but I can't figure out how to use Distinct when I do not have a simple list (a simple list of integers is pretty easy to do, this is not the question). What I ...
1486
votes
17
answers
2.5m
views
Send HTTP POST request in .NET
How can I make an HTTP POST request and send data in the body?
1474
votes
17
answers
1.7m
views
How to calculate difference between two dates (number of days)?
How can one calculate the number of days between two dates in C#?
1472
votes
296
answers
756k
views
Hidden Features of C#? [closed]
This came to my mind after I learned the following from this question:
where T : struct
We, C# developers, all know the basics of C#. I mean declarations, conditionals, loops, operators, etc.
Some ...
1446
votes
31
answers
762k
views
JavaScriptSerializer - JSON serialization of enum as string
I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer value of the enumeration rather than its string "...
1446
votes
22
answers
510k
views
Create Generic method constraining T to an Enum
I'm building a function to extend the Enum.Parse concept that
Allows a default value to be parsed in case that an Enum value is not found
Is case insensitive
So I wrote the following:
public static ...