Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 27th, 2011, 07:47 AM
Authorized User
 
Join Date: Sep 2011
Posts: 24
Thanks: 7
Thanked 0 Times in 0 Posts
Default Query on constructor

Can someone explain whats the need for adding a default constructor(private)
in the card class
Code:
public class Card
    {
        public readonly Rank rank;
        public readonly Suit suit;

public Card(Suit newSuit,Rank newRank )
        {
            suit = newSuit;
            rank = newRank;
        }
        private Card()
        { 
        }
 
Old October 27th, 2011, 08:11 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

The private default constructor can only be invoked within the class Card itself. From outside of the class you can only invoke the constructor with the Suit and Rank parameters.
If the private constructor is not used within the Card class itself it's not needed: if a constructor with parameters is declared, a default constructor is not created anymore.
With the private constructor the Card class could have a method to create a new instance without passing Suit and Rank.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
arun_babu_a (October 28th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:33 AM
static constructor sreenu.pocha C# 1 July 26th, 2006 07:21 AM
Constructor ramess C++ Programming 1 March 18th, 2006 07:23 PM
VB constructor snowbelly VB How-To 2 August 18th, 2004 04:42 AM
Constructor With a Default? n6532l Visual C++ 2 September 18th, 2003 02:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.