Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
A question about constructors
Message
De
23/03/2022 07:20:59
 
 
À
23/03/2022 06:59:35
Information générale
Forum:
C#
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01683956
Message ID:
01683958
Vues:
44
>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform