Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the constructor in C #

Shulou Source: shulou.com Published: 2022-06-01 02:00:44 11月29日 Update

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.

Tags: Functions lines lengths objects clips parameters knowledge facets code content names examples articles results compilation special same value and at the same time members Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB vpn Docker Shulou Tech Info