Note that there are some explanatory texts on larger screens.

plurals
  1. POHow is Java's notion of static different from C#'s?
    primarykey
    data
    text
    <p>I am reading Josh Bloch's book <em>Effective Java</em> and he suggests using a builder design pattern when building objects that have large amounts of members. From what I can see it isn't the vanilla design pattern but looks like his variation. I rather like the look of it and was trying to use it in a C# web application that I am writting. This is the code written in Java and works perfectly</p> <pre><code>public class Property { private String title; private String area; private int sleeps = 0; public static void main(String[] args) { Property newProperty = new Property.Builder("Test Property").Area("Test Area").Sleeps(7).build(); } private Property(Builder builder) { this.title = builder.title; this.area = builder.area; this.sleeps =builder.sleeps; } public static class Builder{ private String title; private String area; private int sleeps = 0; public Builder (String title){ this.title = title; } public Builder Area(String area){ this.area = area; return this; } public Builder Sleeps(int sleeps){ this.sleeps = sleeps; return this; } public Property build() { return new Property(this); } } } </code></pre> <p>When I put this into what I think is the C# equivalent</p> <pre><code> public class Property { private String title; private String area; private Property(Builder Builder) { title = Builder.title; area = Builder.area; } public static class Builder { // Required parameters private String title; private String area; // Optional parameters private int sleeps = 0; public Builder(String val) { this.title = val; } public Builder Area(String val) { this.area = val; return this; } public Builder Sleeps(int val) { this.sleeps = val; return this; } public Property build() { return new Property(this); } } } </code></pre> <p>Then I get compiler warnings. Most of them "cannot declare instance members in a static class".</p> <p>So my question is firstly what have I missed? If I have missed something, can I do it in the manner Josh Bloch recommends but in C#, and lastly, and this one important too, is this thread-safe?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload