Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When going through an article in some website regarding C# object creation, I saw a line that creates an instance using the code
C#
new Class(i);


Where 'i' is already defined.

What does it mean of using a class constructor like above? Is that a possible way or is it wrong ? How this will work? Which class's object is created here? Can anyone help me get some idea? Thanks in advance
Posted
Comments
[no name] 14-Oct-15 2:57am    
Please Share Link Where You See This ... :-)
Sujith Karivelil 14-Oct-15 3:01am    
code wont compile if you create an object like this object o= new class(1);
OriginalGriff 14-Oct-15 3:03am    
Um. Are you sure? :laugh:
Sujith Karivelil 14-Oct-15 3:08am    
why not? it wont compile. throw error, invalid expression term class, but it accept the declaration if we give class-Name instead for class, only if the class having a constructor that accept one argument
OriginalGriff 14-Oct-15 3:46am    
C# is case sensitive: it's perfectly legal to say

public class Class
{
public Class(int count)
{
...
}
}

Stupid, yes - but perfectly legal! :laugh:

When you create a "blank" class, it is given a default constructor by the compiler - one which takes no parameters. It's as if you wrote the class as:
C#
public class MyClass
   {
   public MyClass() {}
   }
But a default constructor isn't the only one you can have - you can have constructors with parameters as well:
C#
public class MyClass
   {
   private int[] myData;
   public MyClass(int count)
      {
      myData = new int[count];
      }
   }
In this case, the constructor allocates the required number of integers in the internal array.
And you then create your class instance like this:
C#
int i = int.Parse(myTextBox.Text);
MyClass mc = new MyClass(i);
 
Share this answer
 
v2
Comments
Maciej Los 14-Oct-15 4:40am    
+5!
It creates an instance of class "Class", which is possible due to "Class" not being the keyword "class". It will be defined somewhere else in the code you're looking at.
The constructor for Class takes an argument whose type i is compatible to. The Class constructor then does something with i that we cannot see in this tiny snippet. Again, have a look at the Class definition code in the originating project.
Finally, the new instance of Class is thrown away unless there's an assignment that's not shown here.
 
Share this answer
 
Means: "Create an instance of the class Class, calling the single parameter class Class' constructor with argument value i".
See Constructors (C# Programming Guide)[^].
 
Share this answer
 
Comments
Maciej Los 14-Oct-15 4:41am    
+5!
CPallini 14-Oct-15 13:27pm    
Thank you.
Monu Thomas 14-Oct-15 5:58am    
I think this may be the correct solution. But there is no definition shown for class 'Class'. Thank you
CPallini 14-Oct-15 13:27pm    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900