C # create and instantiate classes

Source: Internet
Author: User

In C #, instantiation is the process of creating an object. The key word "new" is used to create the object.

In the C # Video, an example is to change the object password. Let's use this example to understand how to create and instantiate a class.

1. Declare a class that can judge whether the password is correct and change the password, and define related methods in the class.

Class Authentic // declare a class that determines whether the PassWord is correct and can change the PassWord {private string PassWord = "zhouzhou"; // define the PassWord public bool IsPasswordCorrect (string userPassword) in the class) // declare the correct method for determining the PassWord in the class {return (PassWord = userPassword )? True: false ;//? : Ternary operator to determine whether it is true} public bool ChangePassWord (string oldPassWord, string newPassWord) // declare the method for changing the PassWord in the class {if (oldPassWord = PassWord) {PassWord = newPassWord; return true;} else return false ;}


2. instantiate the class as follows:

Class Program {static void Main (string [] args) {Authentic simon = new Authentic (); // simon is the name of the authentic instance. Remember to add the bool done brackets after the class; done = simon. changePassWord ("zhouzhou", "zhoujiangxiao"); if (done = true) Console. writeLine ("Password Changed"); else Console. writeLine ("Password Change failed! ");}}

In this example, it is easy to understand that class instantiation is:

Class Name object name = new Class Name ()

3. The access modifier limits class access. :

Programming is very basic, but it is indeed very important.


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.