C # Programming (7_ objects)

Source: Internet
Author: User

A program can create multiple objects of the same class, which are also known as instances. Can be stored in a named variable or stored in an array, in a collection. The code that uses these variables to invoke object methods and public properties is called client code. In object-oriented languages such as C #, typical programs tend to use multiple objects.

Because a class is a reference type, a variable of the class object references the address of the object on the managed heap. If the second object of the same class is assigned to the first object, two objects refer to the object of that address.

The instances of the class are created using the New keyword:

 Public classperson{ Public stringName {Get;Set; }  Public intAge {Get;Set; }  PublicPerson (stringNameintAge ) {Name=name; Age=Age ; }    //Other properties, methods, events ...}classprogram{Static voidMain () {person Person1=NewPerson ("Leopold",6); Console.WriteLine ("Person1 Name = {0} age = {1}", Person1. Name, Person1.        Age); //Declare New Person, assign person1 to it.Person Person2 =Person1; //Change the name of Person2, and Person1 also changes.Person2. Name ="Molly"; Person2. Age= -; Console.WriteLine ("Person2 Name = {0} age = {1}", Person2. Name, Person2.        Age); Console.WriteLine ("Person1 Name = {0} age = {1}", Person1. Name, Person1.        Age); //Keep the console open in debug mode.Console.WriteLine ("Press any key to exit.");    Console.readkey (); }}/*Output:person1 name = Leopold Age = 6 Person2 name = Molly age = + Person1 name = Molly Age =*/

Because structs are value types, a variable of a struct object has a copy of the entire object. Instances of structs can also be created with the new keyword, but this is not required, as follows:

 Public structperson{ Public stringName;  Public intAge ;  PublicPerson (stringNameintAge ) {Name=name; Age=Age ; }} Public classapplication{Static voidMain () {//Create struct instance and initialize by using "new". //Memory is allocated on thread stack.person P1 =NewPerson ("Alex",9); Console.WriteLine ("P1 Name = {0} age = {1}", p1. Name, p1.        Age); //Create new struct object. Note that struct can is initialized//without using "new".person P2 =P1; //Assign values to P2 members.P2. Name ="Spencer"; P2. Age=7; Console.WriteLine ("P2 Name = {0} age = {1}", p2. Name, p2.        Age); //P1 values remain unchanged because P2 is copy.Console.WriteLine ("P1 Name = {0} age = {1}", p1. Name, p1.        Age); //Keep the console open in debug mode.Console.WriteLine ("Press any key to exit.");    Console.readkey (); }}/*OUTPUT:P1 name = Alex Age = 9 P2 name = Spencer age = 7 P1 name = Alex Age = 9*/

The memory of P1 and P2 is allocated on the county stack. The memory is recycled along with the type or method that declares it, which is one reason to copy the structure when the value is assigned, in contrast, when all references to the class instance object are out of scope, the memory allocated for that class instance is automatically reclaimed by the common language runtime (garbage collection). You cannot explicitly destroy a class object as you would in C + +.
When you compare two objects for equality, you must first explicitly want to know whether the two variables represent the same object in memory, or whether the values of one or more of the fields of the two objects are equal. If you want to compare values, you must consider whether the two objects are instances of a value type (struct) or an instance of a reference type (class, delegate, array).

    • To determine whether two type instances refer to the same location in memory (meaning they have the same representation), you can use the static (Equaals) method. (System.Object is an implicit base class for all value types and reference types that contain user-defined structures and classes)
    • To determine whether instance fields in two structure instances have the same values, use the Valuetype.equals method. (because all structures are implicitly inherited from System.ValueType, you can call the method directly on the object, as shown below)
//Person was defined in the previous example.//Public struct person//{//public string Name;//public int age;//Public person (string name, int age)//    {//name = name;//Age = age;//    }//}Person P1=NewPerson ("Wallace", the); Person P2;P2. Name="Wallace";p 2. Age= the;if(P2. Equals (p1)) Console.WriteLine ("P2 and P1 have the same values.");//Output:p2 and P1 have the same values.

The System.ValueType implementation of Equals uses reflection because it must be able to determine which fields are in any structure. When you create your own structure, overriding the Equals method can provide efficient algorithms for the type. To determine whether the values of the fields in the two class instances are equal, you can use the Equals method or the = = operator. However, only classes can use them if they have been overridden or overloaded to provide customizations about the equality meaning of objects of that type. Classes can also implement iequatable<t> interfaces or iequalitycomparer<t> interfaces. Both interfaces provide methods that you can use to test the equality of values.

C # Programming (7_ objects)

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.