C # object-oriented programming for the gentle and newest Learning Series-defining classes and creating class objects (1)

Source: Internet
Author: User

Definition class

In C #, use the class keyword, a name, and a pair of braces to define a new class. The data members and methods of the class are located within the class body (between a pair of braces)

Class syntax format

Class myclass

{

// Subject of the class
}

Class Modifier

When defining a class, you can add an access modifier before the class. The class modifier is used to control the accessed range. By default, the class modifier is internal.

We can see that we added private, protected,All protected internal compilation errors, so only these two class modifiers are supported. One is internal, and the other is public.

1: Internal

The default value is internal when nothing is written.

Only oneProgramCentralized

2: Public

There is no level limit on access members, and the class can be accessed anywhere

The meclass can be instantiated in callmeclass. Cs in the classlibrary1 project. This is because they can be instantiated in a classlibrary1.dll file, that is, they are all in a set of programs.

Running. When we add a public modifier before the class name in the classes Class, we can compile it and break through the restrictions.

Class member Modifier

1: Private

Can only be accessed within the class

2: Internal

Access is limited to a set of Programs

3: Protected

Can only be accessed in the class and the class's derived class, regardless of whether the derived class and the base class are in the same assembly

4:Protected internal protected internal

If it is an inheritance relationship, it can be accessed in the same set of programs. If it is not an inheritance relationship, it can only be accessed in the same set of programs.

5: Public

There is no level limit on access members, and the class can be accessed anywhere

Access permission

 

Create a Class Object

Create a class instantiation object in C # and use the new keyword.

The class we define belongs to the custom type and also the reference type. Therefore, we can instantiate this class with the new keyword.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.