Category Archive for: Objects and Types

Extension Methods

There are many ways to extend a class. U you have the source for the class, then inheritance, which is covered in Chapter 4, is a great way to add functionality to your objects. What if the source code isn’t available? Extension methods can.help by allowing you to change a class without requiring the source code for the…

Read More →

The Object Class

As indicated earlier, all.NET classes are ultimately derived from System. Object. In fact, if you don’t specify a base class when you define a class, the compiler will automatically assume that it derives from Object. Because inheritance has not been used in this chapter, every class you have seen here is actually derived from Sys tern. Obj…

Read More →

Static Classes

Earlier-this chapter discussed static constructors and how they allowed the initialization of static member variables. If a class contains nothing but static methods and properties, the class itself can become static. A static class is functionally the same as creating a class with a private static constructor. An instance of the class can never be created. By using…

Read More →

Partial Classes

The partial keyword allows the class, struct, or interface to span across multiple files. Typically, a class will reside entirely in a single file. However, in situations where multiple developers need access to the same class, or more likely in the situation where a code generator of some type is generating part of a class, then having the…

Read More →

Structs

So far, you have seen how classes offer a great way of encapsulating objects in your program. You have also seen how they are stored on the heap in a way that gives you much more flexibility in data lifetime, but with a slight cost in performance. This performance cost is small thanks to the optimizations of managed…

Read More →

Anonymous Types

When used with the new keyword, anonymous types can be created. An anonymous type is simply a nameless class that inherits from object. The definition of the class is inferred from the initializer, just like in implicitly typed variables. If you needed an object that contained a person’s first, middle, and last name the declaration would look like this:…

Read More →

Class Members

The data and functions within a class are known as the class’s members. Microsoft’s official terminology distinguishes between data members and function members. In addition to these members, classes can contain nested types (such as other classes). All members of a class can be declared as public (in which case they are directly accessible from outside the…

Read More →

Classes and Structs

Classes and structs are essentially templates from which you can create objects. Each object contains data and has methods to manipulate and access that data. The class defines what data and functionality each particular object (called an instance) of that class can contain. For example, if you have a class that represents a customer, it might define fields…

Read More →

Objects and Types

So far, you’ve been introduced to some of the building blocks of the C# language, including variables, data types, and program flow statements, and you have seen a few very short complete programs containing little more than the Main () method. What you haven’t really seen yet is how to put all of these together to form a…

Read More →

Back to Top