1

What must I choose author or Author after this.? Other questions what means the icons beside those options one has a "spanner" and the other has two icons something like a long block more other Little that I don't identify what is that Little and what mean those icons? And if I want to use a second constructor in the same class that is a constructor without arguments is ok to make the constructor like the code after the image?

enter image description here

Other question in the next code Is ok the following constructor without arguments? or can be made that way or is something absurd to do that assignment? if I don't want specific default value would be better to set this.author = null; or this.author = ""; or what must be done?

 public Book()
 {
     this.author = Author;
     this.title = Title;
 }
Vadim Sentiaev
  • 741
  • 4
  • 18
Joe
  • 173
  • 1
  • 2
  • 9
  • There is a big depends on what you are trying to do. Having the constructor set the value through the property or the field are both fine approaches. It depends on what you are doing. Questions like does the property have additional functionality like error checking or calculations, do you want the value to be only set by the constructor and have a read only property. – btlog Sep 13 '15 at 01:01

4 Answers4

4

You should understand what is field and what is property.

Fields - keep your data.

Properties - it's accessors to your data, they like methods but with some sintacsic shugar.

When you have such simple class like Book it can look like this:

class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
}

And you can create new book like this:

var book = new Book {Author = "Name", Title = "Some Book"};

C# has autoproperty, so this code:

public string Title { get; set; }

Is the same as this:

private string title;
public string Title
{
    get { return title; }
    set { title = value; }
}

About your question. In case you like keep your code as is. Constructor should be:

public Book(string title, string author)
{
    this.title = title;
    this.author = author;
}

Basically, when you declare some private field or method it's for your class only.

When you declare public property or method (public fields is bad, do not do this unless absolutly necessary), it's for consumers of your class.

Vadim Sentiaev
  • 741
  • 4
  • 18
  • ok I get it, thanks for your answer, but as I have understood a class can have more than one constructor with different arguments and without arguments if I want to use those 2 constructors(one with arguments and other without args) in the class Book what must assign to "this.author=? " in the constructor without arguments if that constructor don't provide any argument – Joe Sep 13 '15 at 01:16
  • 2
    C# by default will initialize all your fields with default values. For string it will be "null", becouse string is reference type (and all reference types by default will be initialized with null). In case you have parameterless constructor it's up to you. In case you want title and author to be null - do nothing. In case you want to assign some default values, do it. – Vadim Sentiaev Sep 13 '15 at 01:20
  • so is ok the code of that second constructor without parameters? – Joe Sep 13 '15 at 01:24
  • 2
    If you want `author` and `title` to be `null`, then don't assign to them at all or set them to null. `this.author = Author` is useless and makes your code harder to understand. – Fernando Matsumoto Sep 13 '15 at 01:27
3

A constructor without any parameters is called a default constructor.

You don't have to write a default constructor if you don't want to. For example, if you want Book book = new Book (); to be a syntax error then you shouldn't write a default constructor.

If you do create a default constructor then it should initialize your class instance in some way that makes sense in the context of your program. Think about what book should look like after executing Book book = new Book ();.

You could pick some book and author as the default...

  public Book()
  {
     title = "The Hitchhikers Guide to the Galaxy";
     author = "Douglas Adams";
  }

... or you could use some value that makes clear that the instance hasn't really been initialized...

  public Book()
  {
     title = "<<< title not set >>>";
     author = "<<< author not set >>>";
  }

... or you could use some other suitable value ...

  public Book()
  {
     title = String.Empty;
     author = String.Empty;
  }

... or even do nothing...

  public Book()
  {
  }
Frank Boyne
  • 4,400
  • 23
  • 30
  • Picking a default book title maybe quiet strange. By looking `Book book = new Book ();` You don't know that the book is "The Hitchhikers Guide to the Galaxy". Using "<<< title not set >>>" is better in this point of view. – Ken Hung Sep 13 '15 at 02:07
  • 1
    @KenHung The main goal was to demonstrate different uses of the default constructor. I agree in the case of a generalized Book class, picking a specific book as the default probably doesn't make sense. But I could imagine (somewhat strained) examples where it could make sense. While using "<<< title not set >>>" might be better than using a specific book, not creating a default constructor in the first place would probably be best. – Frank Boyne Sep 13 '15 at 16:27
1

In your specific code, the difference of using this.Author or this.author is not much. Generally, I prefer this.Author because later if you add some code in the setter of Book.Author, you may also need to run the additional code, that is why you create a setter.

But your second constructor is wrong:

this.author = Author;

This is equivalent to:

this.author = this.Author

It will assign null to author. You should use the arguments from constructor like your first constructor.

Ken Hung
  • 752
  • 5
  • 13
  • ok thanks for your answer, but as I have understood a class can have more than one constructor with different arguments and without arguments if I want to use those 2 constructors in the class Book what must assign to "this.author=? " in the constructor without arguments if that constructor don't provide any argument – Joe Sep 13 '15 at 00:46
  • "this." is only for access your private non static fields. Also you can call your private non static fields like "_fieldName". In this case you never use "this" and always know when you aceess private non static field. – Vadim Sentiaev Sep 13 '15 at 00:57
  • 1
    @VadimSentyaev `this` can be used to access any instance member (field, property, method) of the current class, be it public, private, protected or internal. Private fields can be accessed with or without the `this` prefix. `this` is usually used when an instance member is hidden by a local variable, but it can be used anywhere to refer to the current object. – Fernando Matsumoto Sep 13 '15 at 01:20
  • @Joe If you want to create a constructor without arguments, you can assign default value to the properties, e.g. `this.Author = "default"`. The value of default depends on what you want to be the default value. Or just don't assign if you don't need a default at all. – Ken Hung Sep 13 '15 at 01:23
  • in the without arguments constructor by " just don't assign if you don't need a default at all" you mean must be as follows? public Book() { this.title; this.author; } – Joe Sep 13 '15 at 01:29
  • 2
    @Joe Just `public Book() { }` – Ken Hung Sep 13 '15 at 01:33
  • @FernandoMatsumoto, yes, you totally can use "this" for access to any instance member, but it does not mean that you should. – Vadim Sentiaev Sep 13 '15 at 18:53
  • @VadimSentyaev indeed, I said `this` could (not should) be used to access any instance member. I also mentioned that the most common use case of `this` when referring to the current object is when an instance member is hidden by a local variable, as supported by http://stackoverflow.com/questions/23250/when-do-you-use-the-this-keyword and https://msdn.microsoft.com/en-us/library/dk1507sz.aspx. The most common use of this is not to refer to private fields. – Fernando Matsumoto Sep 13 '15 at 19:04
0

The spanner represents a Property. The blocks represent a field. Setting a field is more efficient than setting a property. Setting the property requires two operations instead of one. Set the field when you can. Setting a property is useful when something has to occur when the property changes. You can add your OnChange code to the setter of the property.

Lorek
  • 855
  • 5
  • 11
  • ok I understand thanks for your explanation.and what about the code in the constructor without arguments, is ok or wrong? can be assigned the properties to the fields in that constructor or is something absurd? – Joe Sep 13 '15 at 00:25
  • 1
    That code doesn't do anything. The properties are null by default just like the fields. So the assignments are accomplishing nothing. You might want to explicitly set them to "" or something that represents an error condition other than null. But setting the fields to the values of the Properties at that time (during construction) I guess could be called absurd. – Lorek Sep 13 '15 at 06:11