C # Basic Concepts 25 Ask 16-20_c# tutorial

Source: Internet
Author: User
Tags abstract garbage collection readline ipoint
16. What is the difference between class and structure?





For:


Class:





Class is a reference type that is allocated on the heap, and instances of the class are assigned only to duplicate the reference, pointing to the memory allocated by the same segment of the actual object





Class has constructs and destructors





Class can inherit and be inherited





Structure:





The structure is the value type is allocated on the stack (although the stack's access speed is faster than the heap, but the stack's resources are limited), the assignment of the structure will be assigned to generate a new object.





Structs do not have constructors, but they can be added. struct does not have a destructor





Structs may not inherit from another structure or be inherited, but can inherit from interfaces like classes











Example:





Based on the above comparison, we can draw some lightweight objects preferably using the structure, but the data is large or complex processing logic objects best use the class.





For example: Geoemtry (An introduction in GIS, defined in the OGC standard) it is best to use the class, while the members of the Geometry midpoint are best to use the structure





Using System;


Using System.Collections.Generic;


Using System.Text;





Namespace Example16


{


Interface IPoint


{


Double X


{


Get


Set


}


Double Y


{


Get


Set


}


Double Z


{


Get


Set


}


}


Structs can also inherit from interfaces


struct Point:ipoint


{


Private double x, y, Z;


Structs can also increase constructors


Public point (Double X, double Y, double Z)


{


this.x = x;


This.y = y;


This.z = Z;


}


Public double X


{


get {return x;}


set {x = value;}


}


Public double Y


{


get {return x;}


set {x = value;}


}


Public double Z


{


get {return x;}


set {x = value;}


}


}


This simplifies the design of point-like geometry, which also contains complex operations such as project (coordinate transformation).


Class Pointgeometry


{


private point value;





Public Pointgeometry (double X, double Y, double Z)


{


Value = new Point (X, Y, Z);


}


Public Pointgeometry (point value)


{


The assignment of the structure will allocate new memory


This.value = value;


}


Public double X


{


get {return value. X }


set {this.value.x = value;}


}


Public double Y


{


get {return value. Y }


set {this.value.y = value;}


}


Public double Z


{


get {return value. Z }


set {this.value.z = value;}


}


public static pointgeometry operator + (pointgeometry left, Pointgeometry rigth)


{


return new Pointgeometry (Left.x + rigth.x, Left.y + rigth.y, left.z + rigth.z);


}


public override string ToString ()


{


return string. Format ("X: {0}, Y: {1}, Z: {2}", value.) X, value. Y, value. Z);


}


}


Class Program


{


static void Main (string[] args)


{


Point tmppoint = new Point (1, 2, 3);





Pointgeometry tmpPG1 = new Pointgeometry (tmppoint);


Pointgeometry tmpPG2 = new Pointgeometry (tmppoint);


tmppg2.x = 4;


TMPPG2.Y = 5;


Tmppg2.z = 6;





Because structs are value types, the coordinates of TMPPG1 and tmpPG2 are not the same


Console.WriteLine (tmpPG1);


Console.WriteLine (tmpPG2);





Because the class is a reference type, modifying the tmpPG1 coordinates affects the tmpPG3


Pointgeometry tmpPG3 = tmpPG1;


tmppg1.x = 7;


TMPPG1.Y = 8;


Tmppg1.z = 9;


Console.WriteLine (tmpPG1);


Console.WriteLine (tmpPG3);





Console.ReadLine ();


}


}


}


Results:


X:1, Y:2, Z:3


X:4, Y:5, Z:6


X:7, Y:8, Z:9


X:7, Y:8, Z:9








17. What are the problems associated with multiple inheritance of interfaces?





For:





The interfaces in C # are different from classes, and you can use multiple inheritance, that is, a sub-interface can have multiple parent interfaces. But if two parent members have a member with the same name, there is two semantics (which is one of the reasons why classes in C # have canceled multiple inheritance), and it is best to use explicit declarations when implementing





Example:





Using System;


Using System.Collections.Generic;


Using System.Text;





Namespace Example17


{


Class Program


{


An example of a complete interface sound


Interface Iexample


{


Property


String P


{


Get


Set


}


Method


String F (int Value);


Event


Event EventHandler E;


Index indicator


String This[int Index]


{


Get


Set


}


}


Interface IA


{


int Count {get; set;}


}


Interface IB


{


int Count ();


}


IC interface multiple inheritance from IA and IB


Interface Ic:ia, IB


{


}


Class C:ic


{


private int count = 100;


Explicitly declaring the Count property in the IA interface


int IA. Count


{


get {return 100;}


set {count = value;}


}


Explicit declaration implementation of the Count method in the IB interface


int IB. Count ()


{


return count * count;


}


}


static void Main (string[] args)


{


c tmpobj = new C ();





Explicit conversions are also needed when calling


Console.WriteLine ("Count property: {0}", ((IA) tmpobj). Count);


Console.WriteLine ("Count function: {0}", ((IB) tmpobj). Count ());





Console.ReadLine ();


}


}


}


Results:


Count property:100


Count function:10000








18. What is the difference between an abstract class and an interface?





For:





Abstract classes can contain functional definitions and implementations, and interfaces (interface) only contain functional definitions





Abstract class is a concept abstracted from a series of related objects, so it reflects the internal commonness of things. Interfaces are a functional convention defined to satisfy an external invocation, thus reflecting the external nature of Things





Analyze objects, refine internal commonalities to form abstract classes to represent the nature of objects, i.e. "what"





Give preference to interfaces when extending calls or functions for external use








19. What is the alias indicator?





For:





By using an alias designator, we can alias a type





Mainly used to resolve conflicts with the same name type in two namespaces or to avoid using redundant namespaces





The alias designator is defined at the outermost of all namespaces, and the scope is the entire cell file. If defined within a namespace, it works only within a directly subordinate namespace





Example:





Class1.cs:








Using System;


Using System.Collections.Generic;


Using System.Text;





Namespace Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01


{


Class Class1


{


public override string ToString ()


{


Return "Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01 ' s Class1";


}


}


}


Class2.cs:








Using System;


Using System.Collections.Generic;


Using System.Text;





Namespace COM.NBLOGS.REONLYRUN.CSHARP25QEXAMPLE.EXAMPLE19.LIB02


{


Class Class1


{


public override string ToString ()


{


Return "Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02 ' s Class1";


}


}


}


Main unit (Program.cs):





Using System;


Using System.Collections.Generic;


Using System.Text;





Using the alias designator to resolve conflicts of the same type


At the outermost definition of all namespaces, the scope is the entire unit file


Using Lib01class1 = Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;


Using Lib02class2 = Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02.Class1;





Namespace Example19


{


Namespace Test1


{


TEST1CLASS1 is defined within the Test1 namespace, and the scope is within TEST1 only


Using Test1class1 = Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;





Class Class1


{


Lib01class1 and LIB02CLASS2 can be used normally in this


Lib01class1 tmpObj1 = new Lib01class1 ();


Lib02class2 tmpObj2 = new Lib02class2 ();


TestClass1 can be used normally here.


Test1class1 tmpObj3 = new Test1class1 ();


}


}


Namespace Test2


{


Using Test1class2 = Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01.Class1;





Class Program


{


static void Main (string[] args)


{


Lib01class1 and LIB02CLASS2 can be used normally in this


Lib01class1 tmpObj1 = new Lib01class1 ();


Lib02class2 tmpObj2 = new Lib02class2 ();





Note here that TESTCLASS1 is not normally used here.


Because Test1 namespace-defined aliases cannot be used within the Test2 namespace


Test1class1 tmpObj3 = new Test1class1 ();





TestClass2 can be used normally here.


Test1class2 tmpObj3 = new Test1class2 ();





Console.WriteLine (TMPOBJ1);


Console.WriteLine (TMPOBJ2);


Console.WriteLine (TMPOBJ3);





Console.ReadLine ();


}


}


}


}





Results:


Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01 ' s Class1


Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib02 ' s Class1


Com.nblogs.reonlyrun.CSharp25QExample.Example19.Lib01 ' s Class1








20. How to manually release resources?





For:





The. NET platform provides a GC (garbage Collection) for memory management and is responsible for automating the release of managed resources and memory collections. But in the following two situations we need manual resource release: First, because it cannot release unmanaged resources, we must provide ourselves with methods to release the unmanaged resources allocated within the object, such as the use of a COM object in the object's implementation code, and the fact that your class is running with a large number of instances (like GIS geometry), you must manually release these resources to improve the efficiency of your program





Ideally, by implementing an interface that explicitly provides the client with a manual release of the object, there is a IDisposable interface in the System namespace that is appropriate to do so, lest we declare an interface ourselves.


Example:





Using System;


Using System.Collections.Generic;


Using System.Text;





Namespace Example20


{


Class Program


{


Class Class1:idisposable


{


destructor, which is compiled into protected void Finalize (), and the GC calls the method before the object is reclaimed


~class1 ()


{


Dispose (FALSE);


}





By implementing this interface, customers can explicitly release objects without having to wait for the GC to release resources, which is said to be less efficient


void IDisposable.Dispose ()


{


Dispose (TRUE);


}





To design the release of unmanaged resources as a virtual function that provides the ability to free the base class's resources in an inherited class


protected virtual void releaseunmanageresources ()


{


Do something ...


}





Private functions to free unmanaged resources


private void Dispose (bool disposing)


{


Releaseunmanageresources ();





True to indicate that the client explicitly invokes the release function and notifies the GC not to call the object's Finalize method again


is false when the GC calls the Finalize method of the object, so there's no need to tell the GC again you're not going to call my Finalize method.


if (disposing)


{


Gc. SuppressFinalize (this);


}


}


}


static void Main (string[] args)


{


TmpObj1 didn't manually release the resources, just wait for the GC to slowly release it.


Class1 tmpObj1 = new Class1 ();





TmpObj2 called the Dispose method, and legend is more efficient than waiting for the GC to release it.


Personally think it's because you want to view its metadata on an object by case basis to confirm that the Dispose method is implemented.


Of course, the most important thing is that we can determine the time of release to save memory, optimize the efficiency of program operation


Class1 tmpObj2 = new Class1 ();


((IDisposable) tmpObj2). Dispose ();


}


}


}


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.