A Closer Look at C# Classes - Part 1C# has both a constructor and a destructor, which looks and behaves similarly to C++. A class constructor is called when an object is instantiated and the destructor is called when the object is destroyed. Expanding our previous listing, we'll add a constructor and destructor. Listing 2 sealed class Human : Mammal { // -- constructors public Human(string Name) { // -- destructors void Walk( ) { class Example { In C#, having the same name as the class signifies a constructor. You will notice that in Listing 2, there are two constructors for the Human class. C# distinguishes the two constructors by their signature. If you look closer, one of the constructors require a parameter where the other doesn't. When we instantiated an object and passed "Jeff" as the parameter, C# recognizes that the second constructor is requested. In this article, we've dealt with four concepts dealing with classes in C# - abstract, sealed, constructors and destructors. Strings in C# is a lot easier to manage and almost eliminates common mistakes made in C/C++. I've given an insight as to how strings are managed in C# in Listing 2. Copyright © 2001 Jose Aniceto
The copyright of the article A Closer Look at C# Classes - Part 1 in C# Programming is owned by Jose Aniceto. Permission to republish A Closer Look at C# Classes - Part 1 in print or online must be granted by the author in writing.
Go To Page: 1 2 Articles in this Topic Discussions in this Topic |