473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Initialization in Constructor

Hi folks,

In C++ I was able to initialize variables in the constructor like so -

private int a, b, c;

public myClass() : a(0), b(0), c(0)
{

}

Is this possible in C#? Tried it and it didnt seem to work.

Thanks,
David
Jul 3 '06 #1
3 1585
"David++" <Da***@discussions.microsoft.coma écrit dans le message de news:
54**********************************@microsoft.com...

| In C++ I was able to initialize variables in the constructor like so -
|
| private int a, b, c;
|
| public myClass() : a(0), b(0), c(0)
| {
|
| }
|
| Is this possible in C#? Tried it and it didnt seem to work.

Nope, but you can initialise fields in the declaration. Also remember that
all fields are initialised to their "0" value by the constructor anyway, so
ints will always be 0, strings will always be null, etc.

But if you need explicit initialisation to the non-default value, then you
can do this :

public class MyClass
{
private int a = 1;
private int b = 2;
private int c = 3;

public MyClass()
{
//
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 3 '06 #2
"Joanna Carter [TeamB]" wrote:
"David++" <Da***@discussions.microsoft.coma écrit dans le message de news:
54**********************************@microsoft.com...

| In C++ I was able to initialize variables in the constructor like so -
|
| private int a, b, c;
|
| public myClass() : a(0), b(0), c(0)
| {
|
| }
|
| Is this possible in C#? Tried it and it didnt seem to work.

Nope, but you can initialise fields in the declaration. Also remember that
all fields are initialised to their "0" value by the constructor anyway, so
ints will always be 0, strings will always be null, etc.

But if you need explicit initialisation to the non-default value, then you
can do this :

public class MyClass
{
private int a = 1;
private int b = 2;
private int c = 3;

public MyClass()
{
//
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Excellent, that helped a lot. Thanks very much ;-)

David
Jul 3 '06 #3
You can use the readonly keyword which is like the const in C++ and do it this way:

class Test
{
private readonly int a;

Test( int value )
{
a = value;
}

}

www.Noviway.com
Jul 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
17
by: Thomas Lorenz | last post by:
Hello, I'm a little confused about object initialization. According to Stroustrup (The C++ Programming Language, Special Edition, Section 10.2.3) constructor arguments should be supplied in one...
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
6
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.