Hi,

I am going to ask a question which is small bit silly but for me not as i want to grasp this information in mind.
The question is i have seen samples of different codes which have as following

Class A
{
int [] arr=new int[5];
public A()
{
arr={1,8,96,6,3} ;
}
public static meth()
{
//code
}

I dont understand few things in it e.g.

1. Why needed to constructor
2. Why we define array in class and initialize in Constructor?

Class B
{
int [] arr=new int[5];
public B(int i,int j)
{
int lo=i;
int hi=j;
}
public static meth()
{
//code
}

In Class B:

3. Why we pass paramreters and assign to local variables what we think when we do that?


Can you please answer my questions in nice way instead of being angry on my silly questions.I want to undertand this concept fully when to do or not to do like this?

Thanks,
Regds,

Recommended Answers

All 4 Replies

the C# compiler requires a class have a constructor, you don't *have* to use it or even type one if you don't need it. The C# compiler will add a blank constructor with no parameters at compile time if you don't type any constructor. Constructors are good for initialising classes to a certain minimum state of readyness if you like, it is dependant on your project and what you are implementing.

You can have more than one constructor, all must have different signatures (different parameter lists) this allows consumers of your class to instantiate an instance of it in different states again this depends on how complex a class you are creating and the problem you are trying to solve.

Thanks a million.

the C# compiler requires a class have a constructor, you don't *have* to use it or even type one if you don't need it. The C# compiler will add a blank constructor with no parameters at compile time if you don't type any constructor. Constructors are good for initialising classes to a certain minimum state of readyness if you like, it is dependant on your project and what you are implementing.

You can have more than one constructor, all must have different signatures (different parameter lists) this allows consumers of your class to instantiate an instance of it in different states again this depends on how complex a class you are creating and the problem you are trying to solve.

To add to that...

Think of constructors as quite literally that: A method that constructs the class's insides when it is instantiated.
When a class is first called

ClassA myClass = new ClassA();

the very first thing the compiler does is run the constructor.

What I use them for is to setup my fields and what-not so that everything is ready when I need it. Here is an example...

Class A
{
    private string _myField;

    public A()
        {
            _myField = "Called Constructor!";

            MessageBox.Show(_myField.ToString());
        }
}

When you instiantate the class as I showed above (Class A myClass = new Class A) it will print out "Called Constructor!" when you run it!

What hollystyles is talking about when they mention having more than one constructor (which you can have more than one of any method) is called Overloading. It is a little advanced but the simple explanation is that you can have multiple methods within a class so that you can pass different types into it...

Here is an example:

Class A
{
    public A(int x)
    {
        int r = x + 3;

        // This will ouput 3 added to whatever you pass into it
        MessageBox.Show(r.ToString());
    }

    public A(string s)
    {
        // This will output whatever text you pass into it
        MessageBox.Show(s.ToString());
    }

}

Parameters are values that you can pass into and out of a method. So for example if you wanted to add a number (x) to 2 and show the output you would pass x as the parameter to the method.

public int MyMethod(int x) // Notice we put x in there
{
    return = x + 2;
}


... Some code here ...


// Later on when you call your method you pass an integer into it via the variable x

MessageBox.Show(MyMethod(3).ToString());

// This will output the result of MyMethod(), which in this case would be 5

Thanks a million for such a good explaination.Thanks for ur help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.