The Quick & Dirty .NET Guide to C#/VB OOP

Quick Structs

Let's now examine C# structs, or structures as they're referred to in VB. Simply stated, these little bitties act and look just like classes, and utilize all the same kinds of modifiers and elements a class would. You could just think of them as light weight, scaled down classes.

Nevertheless, two caveats though: One, is that they are best suited for smaller data value types under 16 bytes - structures like numbers crunching. Thus, they are used with memory efficiency in mind, as their memory allocation is superior to classes. Two, another key difference in comparison to classes is that they cannot inherit from any other classes as any typical class could, nor could they be a base class either. However, they could implement interfaces though. Also, unlike classes, structs could be instantiated without the new keyword is you so choose. Hmm...

Additionally, classes when instantiated within .NET allocate memory on the heap or .NET's reserved memory space. Whereas structs yield more efficiency when instantiated due to allocation on the stack. Furthermore, it should be noted that passing parameters within structs are done so by value.

A brief word on field initialization and access. Typically, a class field could easily be initialized or preset with a value, as shown:

public class House {
  public string HseColor = "gray"; //This is known as initializing a field
}

However, this won't work for structs unless the fields are static. Whenever we initialize any fields, they're typically known as instance fields/data, whereas the opposite holds true for fields declared with the static keyword. The main difference between the two is that a static field is accessible anywhere in your application without having to instantiate the class containing it. In VB the equivalent keyword is Shared .

[C#]
public class House or struct House {
  public static HseColor = "gray"; //This is known as initializing a field
}

[VB]
Public Class House or Structure House
  Public Shared HseColor As String = "gray"
End Structure

Accessing this kind of field is as simple as:

Response.Write (House.HseColor)

Now getting back to our structs. In the House class as shown earlier, changing the "public class House" to "struct House" make's it now a struct like so:

[C#]
struct House {
  // ... Rest of code here
}

[VB]
Structure House
  ' ... Rest of code here
End Structure

Structs are instantiated in your page in the same manner as classes as we've shown. Again, although our listed struct example works fine as is, keep in mind all the aforementioned limitations and key purposes when implementing them in your programs.

Here we'll now quickly overview classes and other members they could contain and or independently behave as:

  • Fields or Variables - A value that is associated with and within an object or class. Declared with modifiers such as public, private, static, shared, const (constants), etc.
  • Constants - A consistent, unchanged value that is associated with an object or class.
  • Properties - Advanced and more adaptable fields, with get and set accessors.
  • Methods - Encapsulated code performing an action or storing some logic.
  • Classes - A reference type that stores and encloses methods (functions or subroutines), fields, variables or properties, constructors, and even other classes that organize data and perform some kind of functionality.
  • Structs - Light weight, scaled down memory efficient classes.
  • Constructors - creates an instance of a class whenever the object is created.
  • Destructors - destroys the instance of a class.
  • Interfaces - Are a set of specifying arrangements with certain characteristics, that when implemented or inherited in a class must abide by. They could contain all mentioned here.
  • Events - Are sent notifying something of actions going to or taking place.
  • Delegates - Are useful for events and for passing one method or function to another as a parameter. Similar to function pointers in C++.
  • Indexers - these allow you to index classes like you would arrays.
  • Enums - Also known as enumerations , these are a series of constant and related values.

Sorry, if I blew your mind with all these terms. It's good to have an idea what they do although you may never use all of them. Nevertheless, I thought a good passing insight was in order.

I won't go into mind numbing detail on every single aspect of OOP, nor everything listed above, as this is not the article's intent. Rather the aim is, as aforementioned, to present the reader with a quick and dirty introduction and synopsis on all more important commonly used principles of OOP, so you can get a good overall idea and grasp on it.

As you now have a decent grasp on the methodology of objects, classes, and properties, we can now press on and claim our .NET inheritance. :-)

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Beware of bugs in the above code; I have only proved it correct, not tried it.” - Donald Knuth