11

I have the following class:

public class Topic
    {
        public string Topic { get; set; }
        public string Description { get; set; }
        public int Count { get; set; }
    }

I would like to have the Count always set to zero when the class is created with the following:

var abc = new Topic {
  Topic = "test1",
  Description = "description1"
}

I am a bit confused with constructor. Is this possible or do I need to specify Topic, Description and Count when I create abc?

6 Answers 6

16

The default value of an int is 0.

All value types have default values as they can't be null.

See Initializing Value Types on this MSDN page.

Sign up to request clarification or add additional context in comments.

4 Comments

This is good news for me. How about strings and bool. Do they default to "" and false ?
@Geraldo - No. Strings are reference types, so they default to null.
Is there a way that I could set a string to default to something or do I have to specify each string?
@Geraldo - You can use a parameterless constructor, but you will still need to set each string in turn. There is not magical way to change such defaults.
11

You have a few different options.

1) int defaults to zero so it will be zero if you dont initialize it.

2) you can use a constructor

public Topic(){ Count = 0;}

3) You can use a backing field instead of auto-property and initialize that to zero

 private int _count = 0;
 public int Count {
    get  {return _count}
    set {_count = value; }
 }

Comments

7

Count will default to 0 on initialisation, since it is a value type and can't be null.

Comments

5

This following idiom is not only a constructor:

var abc = new Topic {
  Topic = "test1",
  Description = "description1"
}

It's a constructor and an object initializer.

What really happens is that new Topic() is called first, hence initializing all values to their defaults (property Topic is null, Description is null and Count is 0). After that, the value "test1" is assigned to Topic, and the value "description1" is assigned to Description.

All value types have a default value different than null (since they can't be null), and reference types default to null.

Comments

0

public class Program { public static void Main() {

    // Declare a StudentName by using the constructor that has two parameters.
    StudentName student1 = new StudentName("Craig", "Playstead");

    // Make the same declaration by using a collection initializer and sending 
    // arguments for the first and last names. The default constructor is 
    // invoked in processing this declaration, not the constructor that has
    // two parameters.
    StudentName student2 = new StudentName
    {
        FirstName = "Craig",
        LastName = "Playstead",
    };

    // Declare a StudentName by using a collection initializer and sending 
    // an argument for only the ID property. No corresponding constructor is
    // necessary. Only the default constructor is used to process object 
    // initializers.
    StudentName student3 = new StudentName
    {
        ID = 183
    };

    // Declare a StudentName by using a collection initializer and sending
    // arguments for all three properties. No corresponding constructor is 
    // defined in the class.
    StudentName student4 = new StudentName
    {
        FirstName = "Craig",
        LastName = "Playstead",
        ID = 116
    };

    System.Console.WriteLine(student1.ToString());
    System.Console.WriteLine(student2.ToString());
    System.Console.WriteLine(student3.ToString());
    System.Console.WriteLine(student4.ToString());
}

// Output:
// Craig  0
// Craig  0
//   183
// Craig  116

}

public class StudentName {

// The default constructor has no parameters. The default constructor 
// is invoked in the processing of object initializers. 
// You can test this by changing the access modifier from public to 
// private. The declarations in Main that use object initializers will 
// fail.
public StudentName() { }

// The following constructor has parameters for two of the three 
// properties. 
public StudentName(string first, string last)
{
    FirstName = first;
    LastName = last;
}

// Properties.
public string FirstName { get; set; }
public string LastName { get; set; }
public int ID { get; set; }

public override string ToString()
{
    return FirstName + "  " + ID;
}

}

Comments

0

EDIT

As I learned from the comments to this answer, it is perfectly valid to leave out the () in the initializer call.

The correct syntax would be My preferred syntax still is:

var abc = new Topic() {
  Topic = "test1",
  Description = "description1"
}

(note the ()).

This would initialize Count to 0, as 0 is the default value for int. In case you want to always specify Topic and Description, add an explicit constructor:

public Topic(string topic, string description)
{
    Topic = topic;
    Description = description;
    // You may also set Count explicitly here, but if you want "0" you don't need to
}

5 Comments

Huh?? The OP didn't put an empty argument list in his "constructor call" - I pointed him to this and, as I can not make source code bold, noted my change so that it is easier for him to see. That gives me a -1?
@Thorsten Dittmar: Whether you have () or not in the syntax, it makes no difference whatsoever. Check the IL.
That was indeed new to me. While it is not required, one might still consider it good style. In that case, "The correct syntax" should be changed to "My preferred syntax". Still, I'm glad to see that a minor thing like that - which is not directly related to answering the question and could easily have been explained by writing a more "elaborate comment" - leads to an immediate downvote. That's how things should work.
I think the thing that led to the downvote was your incorrectness, which seems reasonable to me. Note that I haven't voted on your answer. And I've never heard of anyone saying that omitting the optional parens is not good style.
Well, I can't omit them for "normal methods", so to be consistent with th ... never mind...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.