Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use inner class and Lambda expression in C #

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use inner classes and Lambda expressions in C#. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

First, internal class 1. What is an inner class? 1. Class as formal parameters and return values:

Abstract classes as formal parameters and return values:

The method of passing the abstract class as the formal parameter through the form of polymorphism, and the return value of the shape participation of the method is the abstract class name, then what is needed is the subclass object of the abstract class.

The class name is used as a shape to participate in the return value: when the shape participation return value of the method is the class name, what is needed is the object of the class.

The interface name is used as the shape participation return value: when the shape participation return value of the method is the interface name, what is required is the implementation class object of the interface.

two。 An overview of inner classes:

The inner access feature of the inner class is that when the outer class needs to access the inner class, it needs to create an object to call it, and the inner class can directly use the members of the outer class.

two。 Several forms of inner class

1. Member inner class

Note: when creating an object of an inner class, in addition to a private inner class, when it is defined as a private class, you need an external class to create an object to call the members and methods of the internal class, and then create an object of the external class in the test class. call its method.

two。 Local inner class

The local inner class needs to be written in the method, and the main format is similar to that of the member inner class.

Anonymous inner class (a format of local inner class, which also needs to be written in the method)

When you need an inner class of an interface and an abstract class, you can use the anonymous inner class. Note that the anonymous inner class is an object that inherits the class equivalent to the implementation of the interface or abstract class, so you can directly assign a value to an interface, abstract name, and directly call the members of the inner class.

When used in development:

First of all, an object is created, the method in it is called after the object is created, and the return value is an interface, so you need to create an interface class to override the interface method and call it. However, the anonymous inner class reduces a lot of steps, and it is easy to rewrite the methods in the interface by means of anonymous inner class in the test class.

Second, Lambda expressions (emphasizing what to do, not in what form) the format requirements of 1.Lambda expressions:

Note: prerequisites for Lambda expressions:

There is an interface

There is one and only one abstract method in the interface.

Example:

Note that a method is written below. When the method is called, it will be run in the expression that returns Lambda and then returned to perform the next step.

The difference between 2.Lambda and anonymous inner classes 1. Ellipsis mode

Parameters can be omitted (multiple parameters need to be omitted), parentheses can also be omitted if there is only one parameter, and curly braces and semicolons can also be omitted if there is only one code block statement (Note: if there is return, return should also be omitted).

Note:

The difference between 2.Lambda and anonymous inner classes:

3. Method reference 1. An overview of the method:

In fact, the method reference is the twin brother of Lambda, in the form of method reference, the method reference character is "::". In the form of method name (System.out::println), it is actually the parameter in the written Lambda expression that is assigned and called again. So the method reference is mainly applied to the secondary application of the written Lambda expression.

two。 Common method references are as follows:

3. How to refer to the method:

Instance method of referencing object

Description: in the case of referencing the instance method of the object, the Lambda expression is the same as the operation performed in the class, so the substitution can be used. in the substitution, all its formal parameters are passed to the method as parameters in the format of "object name:: method name".

Instance method of reference class

Description: when referencing the instance method of a class, it is required by the format of "class name:: method name".

Reference constructor (reference constructor)

Note: when referencing the constructor, it is done through the format of "class name:: new". When the Lambda expression is replaced by the constructor, all its formal parameters are passed to the constructor as parameters. At the same time, the passed parameters enter into the constructor with two parameters through the class name.

Reference class method (static method of reference class)

Description: when you need to reference a class method, there is a "class name:: static method name" format to modify at the same time, when the Lambda expression is replaced by the class method, all its formal parameters are passed to the static method as parameters.

The above is all the content of the article "how to use inner classes and Lambda expressions in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report