In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use the constructor in C#, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the constructor in C#. Let's take a look.
Constructor in C #
The constructor of a class is a special member function of the class that is executed when a new object of the class is created.
The name of the constructor is exactly the same as the name of the class, and it does not have any return type.
The following example illustrates the concept of a constructor:
Using System
Namespace LineApplication
{
Class Line
{
Length of private double length; / / Lin
Public Line ()
{
Console.WriteLine ("object created")
}
Public void setLength (double len)
{
Length = len
}
Public double getLength ()
{
Return length
}
Static void Main (string [] args)
{
Line line = new Line ()
/ / set the length of the line
Line.setLength (6.0)
Console.WriteLine ("Line length: {0}", line.getLength ())
Console.ReadKey ()
}
}
}
When the above code is compiled and executed, it produces the following results:
The length of the line created by the object: 6
The default constructor does not have any arguments. But if you need a constructor with parameters that can have arguments, this constructor is called a parameterized constructor. This technique can help you assign initial values to objects while creating them. For more information, please see the following example:
Using System
Namespace LineApplication
{
Class Line
{
Length of private double length; / / Lin
Public Line (double len) / / parameterized constructor
{
Console.WriteLine ("object created, length = {0}", len)
Length = len
}
Public void setLength (double len)
{
Length = len
}
Public double getLength ()
{
Return length
}
Static void Main (string [] args)
{
Line line = new Line (10.0)
Console.WriteLine ("Line length: {0}", line.getLength ())
/ / set the length of the line
Line.setLength (6.0)
Console.WriteLine ("Line length: {0}", line.getLength ())
Console.ReadKey ()
}
}
}
When the above code is compiled and executed, it produces the following results:
Object has been created, length = 10 line length: 10 line length: 6 on "how to use the constructor in C#" this article is introduced here, thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use the constructor in C#". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.