Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
A question about constructors
Message
From
23/03/2022 07:20:59
 
 
To
23/03/2022 06:59:35
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01683956
Message ID:
01683958
Views:
43
>Not sure if I understand correctly the use of constructors.
>
>Is it a way to make sure that a class will always have parameters initialized the right way?
>
>For example if I don't create a constructor and the class has two parameters that should be integers. it could be called with anything as parameters.
>
>But if I create a constructor for that class and I don't specify these parameters then the editor will tell me about my error?

Yes
If you don't specify a constructor without parameters, the IDE will complain - if it is a class
It will not complain if it is a structure, since a structure has a parameterless constructor by default
* COMPLAIN
	internal static class Constructor_Test
	{
		internal static  void Go()
		{
			Constructor c = new Constructor();
		}
	}

	internal class Constructor
	{
		int X;
		int Y;
		
		internal Constructor(int x, int y)
		{
			X = x;
			Y = y;

		}
	}
&& NO COMPLAINT
	internal static class Constructor_Test
	{
		internal static  void Go()
		{
			Constructor c = new Constructor();
		}
	}

	internal struct Constructor
	{
		int X;
		int Y;
		
		internal Constructor(int x, int y)
		{
			X = x;
			Y = y;

		}
	}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform