What is the difference between a class and a struct in C#?
Simple meaning
Classes are reference types on the heap
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready .NET / C# questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
Classes are reference types on the heap
Open the full page for Why, Steps, Example and Key takeaway.
Properties expose get and set accessors like fields but with control.
Open the full page for Why, Steps, Example and Key takeaway.
var still has a compile-time type
Open the full page for Why, Steps, Example and Key takeaway.
It lets a method accept a variable number of arguments as an array.
Open the full page for Why, Steps, Example and Key takeaway.
Enums give named integer constants for a closed set of values.
Open the full page for Why, Steps, Example and Key takeaway.
throw keeps the original stack trace when rethrowing.
Open the full page for Why, Steps, Example and Key takeaway.
List grows dynamically and has helpful methods.
Open the full page for Why, Steps, Example and Key takeaway.
Any can stop at the first match.
Open the full page for Why, Steps, Example and Key takeaway.
It overrides base appsettings for the Development environment.
Open the full page for Why, Steps, Example and Key takeaway.
It redirects HTTP requests to HTTPS.
Open the full page for Why, Steps, Example and Key takeaway.
Both persist tracked changes
Open the full page for Why, Steps, Example and Key takeaway.
Constructor injection makes required dependencies obvious and testable.
Open the full page for Why, Steps, Example and Key takeaway.
Interpolation is readable and compile-checked for expressions.
Open the full page for Why, Steps, Example and Key takeaway.
It holds local launch profiles, URLs, and env vars for Visual Studio and dotnet run.
Open the full page for Why, Steps, Example and Key takeaway.
It stops a dependency from flowing transitively to consumers.
Open the full page for Why, Steps, Example and Key takeaway.
A value-type record with value equality semantics.
Open the full page for Why, Steps, Example and Key takeaway.
Boxing wraps a value type in an object on the heap
Open the full page for Why, Steps, Example and Key takeaway.
string is immutable so each change may allocate a new string.
Open the full page for Why, Steps, Example and Key takeaway.
It lets you write asynchronous methods with await.
Open the full page for Why, Steps, Example and Key takeaway.
It flags possible nulls at compile time when nullable context is on.
Open the full page for Why, Steps, Example and Key takeaway.