


We have defined the YSchool and YTeacher classes. When instantiating the object:
YSchool shool1 = new YSchool(); shool1.ID = 1; shool1.Name = "清华附中"; YSchool school2 = new YSchool(); school2.ID = 2; school2.Name = "北师大附中";
Is this correct? In fact, it is logically incorrect, because when the object is instantiated, its attributes should be and It is there at the time of instantiation, rather than adding attributes later. The performance in the program is that the attributes must have initial values.
So, there must be such a method in the class, with no return type, the method name is the same as the class name, and there is a parameter class table or no parameter list. It is the construction method, commonly known as "constructor" or "constructor". A class can have one or more constructors. Of course, sometimes you don't write a constructor. This does not mean that the class does not have a constructor. It still has a default constructor. If multiple constructors are used, their respective parameter lists must be different.
The following uses YSchool as an example to complete this class.
/// <summary> /// YSchool类的id和name是它的固有属性,它的值应该是确定的。 /// 实例化的同时对属性赋初值,用到构造函数。 /// </summary> public class YSchool { private int id = 0; private string name = string.Empty; public int ID { get { return this.id; } } public string Name { get { return name; } } /// <summary> /// 没有参数的构造器称之为“默认构造器”;如果没有写一个构造器,那么 ///系统也会提供一个默认的构造器,所以说类至少有一个构造器; ///当然,如果系统提供的默认构造函数,则属性初值为声明时赋的初值,如果声明 ///未赋初值,则是“类型默认值”,比如0或者null。 /// </summary> public YSchool() { this.id = 0; this.name = @"清华附中"; } /// <summary> /// 带有参数列表的构造函数, /// 属性的值就是传入的列表的值。 /// </summary> /// <param name="id"></param> /// <param name="name"></param> public YSchool(int id, string name) { this.id = id; this.name = name; } public YSchool(int id) { this.id = id; this.name = @"陕科大附中"; } }
class Program { static void Main(string[] args) { YSchool shool1 = new YSchool(); YSchool school2 = new YSchool(1, @"西工大附中"); YSchool school3 = new YSchool(2); Console.ReadKey(); } }
The get/set attributes of id and name have also been modified above to only get. Because these attribute values are inherent attributes, it is not logical to assign values after instantiation. That is, these properties are read-only.
The default value is mentioned in the code, let’s briefly talk about it here. When declaring a field, you can use the assignment operator "=" to directly add a value to the field, such as
string name = string.Empty;
This is not assigning a value to a variable. The default value is just a form. The actual assignment of a variable is still in the constructor. middle. Generally, standardized codes require that variables be assigned initial values when declaring them. If there is no assigned field during life, the compiler will add an assignment code to assign the field to the set default value. In fact, no matter whether we add a default value to the field, the field has a default value, but if we do not add a default value artificially, the default value of the field will be 0 or null.
One more thing to note here is that the system default constructor was mentioned earlier, but when the constructor is defined, the system default constructor does not exist. Therefore, if in some cases, both the default constructor and the constructor with parameters are used, the default constructor must be manually defined. For example,
public YSchool() { }
or
public YSchool() { this.id = 0; this.name = @"尚进"; }
In summary, the canonical expression for instantiating a class should be: class name instance name = new class name (constructor parameter list);
The above is the compilation of basic knowledge of C#: basic knowledge (3) class structure. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)