Archive for the ‘Tutorials’ Category

C# Programming #2: Type Overview

August 10, 2010 Leave a comment

The C# programming language is a strongly-typed language. Meaning that every variable, every class, every constant is a Type, including any expression that evaluates into a value. Every method in C# uses Types as return values, and method parameters supplied are Types.

The .NET Framework library includes hundreds of classes that represent a wide variety of simple and complex Types. Wait, a class represents a Type? What’s that mean? Well, a class encapsulates your source code, it contains all the code that makes your Type work. Think of it in this light, a class is the blueprint, a Type is the finish product.

You probably know that most software is compiled into 1’s and 0’s right? Well Microsoft has done something interesting, they’ve create an intermediary of sorts. While your programming language of choice might be C# or Visual Basic.NET, there is yet one more language included in the .NET Framework and that’s the Common Intermediate Language (or CIL for short).
CIL is not 1’s and 0’s, but rather byte code. It’s still humanly readable, (albeit not easily understandable), and is what is eventually compiled into 1’s and 0’s. At first this seems like an extra step, but I’m going to explain why it’s done this way.
So, you’re language of choice is C#? You compile your application and it gets compiled into CIL code. What if your friend makes an add-on for your application, but he wrote it in VB? His add-on will still be compatible with your C# application due to the fact that his VB source code will be compiled into CIL as well. Once the C# and the VB codes are in CIL, they are considered equal, they are the same language. Even though your .exe file is sitting there looking at you, it’s not really in 1’s and 0’s for the computer to read, it’s in CIL. When you start your application, the .NET Framework immediately compiles the CIL code into 1’s and 0’s, so that your application runs as a Native app.

What’s this have to do with Types? Well, like I mentioned before, a class contains all your source code, it acts as a blueprint. When you compile your source code into CIL, the class becomes a compiled Type. A finished product. What’s this mean for you? It means that your finished product, your Type, can now be used within your software or anyone else’s software. What if you are writing your application, and you write a class, and you need to use your class for your application, does it need to be compiled separately from your program in order to be used? Not at all! For an example, lets assume we have a class called Car and your program wants to use the Car to let people drive it. You have the class, but you don’t have a compiled Type yet, not only that, but the Car is part of your program and thus you can’t get access to the compiled Type right? You only have access to the class? (blueprint in source). Well that’s actually incorrect, as the C# compiler will see that your application needs to use the Car class, and will go ahead and compile it into a Type while it compiles the rest of your program, allowing your compiled program to use the Type internally.

Hopefully I did a decent job of describing the difference between Types and Classes, if not, feel free to drop a comment below. Just remember as we go through the remaining tutorials in this series, that when I mention class, it refers to your source code, and when I say Type, it refers to the actual finished object.

I will explain more on Types as I get into creating Properties, Fields and Methods.

Learning C# course outline

August 4, 2010 Leave a comment

I sat down today and built a course list for my Programming C# tutorial series I’m working on. The following is the current layout of what I will be going over. These are in the order that i will be writing them in, any other areas you guys could recommend?

.NET Overview
Type overview
.NET Virtual Machine
Fields vs Properties
Conditional Statements
Methods (Params & Return Values)
.NET Stack & Heap Overview
Reference & Value Types (ref & out)
.NET Garbage Collector
Type Casting
Is & As Keywords
Dynamic Types
System.Convert Method
Enum Types
The .NET Library Review
Files & Directories
Looking for what Type to use
Reflection Overview
Reflection: Assemblies
Reflection: Instancing
Multi Threading
Application Domains
Win Forms Introduction
Win Forms: Controls
Win Forms: Events
Win Forms: Dynamic UI (reflection)

C# Programming #1: Overview of .NET

August 3, 2010 Leave a comment

If you go online and do a search for .net examples, you’re going to find tutorials & source code that shows you how to do what it is that you want your software to do. For example, if you need to store data and access it later, most documentation shows how to store the data, but it does not go into why it stores the data the way they tell you to, or if storing data as such could lead to a potential issue with memory.
This is a common problem a lot of writers have, they place some source on the Internet, tell you it works and move on. That’s giving you the answer without teaching you how they got that answer in the first place. How can you learn from that?
Having said that, I’m going to start putting together documentation that goes into detail regarding the language of C# and how the .NET Framework co-exists with it. My experience over the last few years has shown that most people just ask on forums how to perform certain tasks, because they don’t understand the language itself. I saw someone post one time asking how to make a custom message box for a console application as their console app could not find the standard windows form message box. Someone mentioned that you can simply add the needed .dll to your project and it would become useable; sure enough, it fixed his problem.
If the user understood the framework, they would know that .NET GUI components are stored in a separate library and can be used with Console Applications.
The goal of this training series is to cover both the framework and the language, to teach users how to understand what it is that they’re writing.

Moving on now, I want to briefly touch on the .NET Framework, I will be going over this in more detail in the next entry.
The .NET Framework is a collection of code components used to run the code. It doesn’t just run it’s own code however, it runs your code too. The Framework contains pre-existing code for various things such as User Interfaces, Database Connectivity, Data Access, File Input/Output, Networking, Numerical Algorithms and Web Development. Developers can take this pre-existing code and use it within their applications, preventing them from having to re-invent the wheel.
The Framework also includes a Virtual Machine, a runtime compiler, a memory manager and a native image generator. Each one of these will be explained in greater detail at a later date.
With .NET, software can be rapidly prototyped thanks to the existence of the framework and it’s pre-existing code you can use. You won’t have as much debugging to do, and you can quickly put together a proof of concept without needing to start from scratch. It’s fast and system friendly provided the system has the correct version installed.

Developing in .NET isn’t to difficult, and it supports a variety of languages such as C#, C++, Visual Basic, F#, Ruby and Python. If you don’t like any of those languages, you can build your own to support it, it’s surprisingly easy and Microsoft even documents how to do it.

So hopefully you now understand what the framework is essentially, a collection of pre-wrote source files that make development easier and faster. Over the next few posts I will go over how it works; describing the process that occurs when you compile your source, run it and how the various framework components work.