NEWBIE: Initialization of member variables 
Author Message
 NEWBIE: Initialization of member variables

Hello again to you all,

In C++ you have colon initialization, ie

class myclass
{
    int one;
    int two;

    myclass():one(6), two(9)
    {
    }

Quote:
}

The significance is that temporary objects are not created, etc.

In C#, when you have class instance variables. Is it better to initialize
them when you create them:

class mysharpclass
{
    int one = 3;
    int two = 9;

Quote:
}

or is it better to initialize in the constructor:

class mysharpclass
{
    int one;
    int two;

    mysharpclass
    {
        one = 3;
        two = 9;
    }

Quote:
}

Thanks, JamesO
Note: Please only reply to group as I'm trying to fight spam.


Sat, 18 Jun 2005 06:29:58 GMT  
 NEWBIE: Initialization of member variables
James, for all intents and purposes (someone correct me if I'm wrong) these
are the same.  Your class will not be initialized until you create it and to
create a class you always call the constructor.  So, for a simple scenario
like yours your variables will be initialized at the same time in both
scenarios.

Personally (I think this is an MS best practice as well) I like to
initialize variables when they are declared (your first option).  This way
you can test against their value later to be sure that the class was setup
appropriately.  For instance, if you set them both to -1 when you declared
them you could do a test in your constructor to make sure they were
initialized to some value other than -1.  If they weren't you know something
happened.  In this simple example that doesn't provide much if any value but
in a more complex example it might make a difference.

--
Greg Ewing [MVP]
http://www.claritycon.com/


Quote:
> Hello again to you all,

> In C++ you have colon initialization, ie

> class myclass
> {
>     int one;
>     int two;

>     myclass():one(6), two(9)
>     {
>     }
> }

> The significance is that temporary objects are not created, etc.

> In C#, when you have class instance variables. Is it better to initialize
> them when you create them:

> class mysharpclass
> {
>     int one = 3;
>     int two = 9;
> }

> or is it better to initialize in the constructor:

> class mysharpclass
> {
>     int one;
>     int two;

>     mysharpclass
>     {
>         one = 3;
>         two = 9;
>     }
> }

> Thanks, JamesO
> Note: Please only reply to group as I'm trying to fight spam.



Sat, 18 Jun 2005 07:28:56 GMT  
 NEWBIE: Initialization of member variables
Great, thanks, just what I was looking for.
JamesO



Quote:
> James, for all intents and purposes (someone correct me if I'm wrong)
these
> are the same.  Your class will not be initialized until you create it and
to
> create a class you always call the constructor.  So, for a simple scenario
> like yours your variables will be initialized at the same time in both
> scenarios.

> Personally (I think this is an MS best practice as well) I like to
> initialize variables when they are declared (your first option).  This way
> you can test against their value later to be sure that the class was setup
> appropriately.  For instance, if you set them both to -1 when you declared
> them you could do a test in your constructor to make sure they were
> initialized to some value other than -1.  If they weren't you know
something
> happened.  In this simple example that doesn't provide much if any value
but
> in a more complex example it might make a difference.

> --
> Greg Ewing [MVP]
> http://www.claritycon.com/



> > Hello again to you all,

> > In C++ you have colon initialization, ie

> > class myclass
> > {
> >     int one;
> >     int two;

> >     myclass():one(6), two(9)
> >     {
> >     }
> > }

> > The significance is that temporary objects are not created, etc.

> > In C#, when you have class instance variables. Is it better to
initialize
> > them when you create them:

> > class mysharpclass
> > {
> >     int one = 3;
> >     int two = 9;
> > }

> > or is it better to initialize in the constructor:

> > class mysharpclass
> > {
> >     int one;
> >     int two;

> >     mysharpclass
> >     {
> >         one = 3;
> >         two = 9;
> >     }
> > }

> > Thanks, JamesO
> > Note: Please only reply to group as I'm trying to fight spam.



Sun, 19 Jun 2005 01:33:17 GMT  
 NEWBIE: Initialization of member variables

Quote:

> James, for all intents and purposes (someone correct me if I'm wrong) these
> are the same.  Your class will not be initialized until you create it and to
> create a class you always call the constructor.  So, for a simple scenario
> like yours your variables will be initialized at the same time in both
> scenarios.

There's *one* difference that I can think of, but one that should be
avoided anyway. Here are two example programs:

using System;

public abstract class Base
{
    public Base()
    {
        ShowX();
    }

    public abstract void ShowX();

Quote:
}

public class Derived : Base
{
    int x = 1;

    // Unnecessary but retained for consistency
    public Derived()
    {
    }

    public override void ShowX()
    {
        Console.WriteLine (x);
    }

    public static void Main ()
    {
        new Derived();
    }

Quote:
}

The above prints "1".

Now here's the second program:

using System;

public abstract class Base
{
    public Base()
    {
        ShowX();
    }

    public abstract void ShowX();

Quote:
}

public class Derived : Base
{
    int x;

    public Derived()
    {
        x = 1;
    }

    public override void ShowX()
    {
        Console.WriteLine (x);
    }

    public static void Main ()
    {
        new Derived();
    }

Quote:
}

That prints 0.

It's generally a bad idea to call virtual methods in a constructor, as
subclasses may not be completely initialised yet.

--
Jon Skeet

If replying to the group, please do not mail me at the same time



Sun, 19 Jun 2005 19:54:57 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. static member variable initialization

2. newbie's base struct member initialization question

3. Newbie: static const member initialization

4. Newbie - Control ID's not showing on member variables tab

5. Newbie problem with Member Variable and validated range

6. member function and member variable

7. static function access member variable and member function

8. member function and member variable

9. member function and member variable

10. struct initialization (with static, const static members)

11. union members initialization

12. Initialization problem with structure members

 

 
Powered by phpBB® Forum Software