Click here to Skip to main content
15,499,046 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Another ancient oddity in .net Pin
Thornik12-Sep-22 9:26
MemberThornik12-Sep-22 9:26 
GeneralRe: Another ancient oddity in .net Pin
Dave DD12-Sep-22 9:45
MemberDave DD12-Sep-22 9:45 
GeneralRe: Another ancient oddity in .net Pin
F Margueirat12-Sep-22 12:02
MemberF Margueirat12-Sep-22 12:02 
GeneralRe: Another ancient oddity in .net Pin
obermd12-Sep-22 4:58
Memberobermd12-Sep-22 4:58 
GeneralRe: Another ancient oddity in .net Pin
PIEBALDconsult12-Sep-22 5:35
professionalPIEBALDconsult12-Sep-22 5:35 
GeneralRe: Another ancient oddity in .net Pin
Gerry Schmitz12-Sep-22 8:35
mveGerry Schmitz12-Sep-22 8:35 
GeneralC# 6, you dirty dog! Pin
PIEBALDconsult27-Aug-22 10:57
professionalPIEBALDconsult27-Aug-22 10:57 
GeneralRe: C# 6, you dirty dog! Pin
Richard Deeming29-Aug-22 22:15
mveRichard Deeming29-Aug-22 22:15 
PIEBALDconsult wrote:
it's a coding error
That depends on your intention.

If you wanted a property which could be modified by any code within your class, then yes.

If you wanted a true "read-only" property, which can only be initialized in the constructor or an initializer, then omitting the private set; is perfectly correct.
C#
public class Foo
{
    public int Bar { get; private set; }
    public int Baz { get; }
    
    public Foo(int bar, int baz)
    {
        Bar = bar; // Fine
        Baz = baz; // Also fine
    }
    
    public void Groot()
    {
        Bar = 42; // Compiles and runs
        Baz = 42; // Compiler error CS0200 - Property or indexer 'Foo.Baz' cannot be assigned to -- it is read-only
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: C# 6, you dirty dog! Pin
PIEBALDconsult30-Aug-22 4:35
professionalPIEBALDconsult30-Aug-22 4:35 
GeneralRe: C# 6, you dirty dog! Pin
James Curran1-Sep-22 1:04
MemberJames Curran1-Sep-22 1:04 
GeneralRe: C# 6, you dirty dog! Pin
jochance1-Sep-22 6:47
Memberjochance1-Sep-22 6:47 
GeneralRe: C# 6, you dirty dog! Pin
englebart1-Sep-22 7:55
professionalenglebart1-Sep-22 7:55 
GeneralRe: C# 6, you dirty dog! Pin
PIEBALDconsult1-Sep-22 8:00
professionalPIEBALDconsult1-Sep-22 8:00 
GeneralTwo of my most frequent issues with .net inconsistencies Pin
PIEBALDconsult24-Aug-22 11:28
professionalPIEBALDconsult24-Aug-22 11:28 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
MarkTJohnson24-Aug-22 11:31
professionalMarkTJohnson24-Aug-22 11:31 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
Greg Utas24-Aug-22 12:29
mveGreg Utas24-Aug-22 12:29 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
PIEBALDconsult24-Aug-22 12:37
professionalPIEBALDconsult24-Aug-22 12:37 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
Greg Utas24-Aug-22 14:35
mveGreg Utas24-Aug-22 14:35 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
englebart1-Sep-22 7:59
professionalenglebart1-Sep-22 7:59 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
Richard Deeming24-Aug-22 23:06
mveRichard Deeming24-Aug-22 23:06 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
PIEBALDconsult25-Aug-22 4:21
professionalPIEBALDconsult25-Aug-22 4:21 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
Richard Deeming25-Aug-22 6:05
mveRichard Deeming25-Aug-22 6:05 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
PIEBALDconsult25-Aug-22 8:40
professionalPIEBALDconsult25-Aug-22 8:40 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
Bernhard Hiller25-Aug-22 22:09
MemberBernhard Hiller25-Aug-22 22:09 
GeneralRe: Two of my most frequent issues with .net inconsistencies Pin
PIEBALDconsult27-Aug-22 7:19
professionalPIEBALDconsult27-Aug-22 7:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.