Learning C#: How to Master an Object Oriented Programming Language
If you’ve worked with any C-style language, C# will come as second nature to you as a programmer. C# is extremely similar to C++ and especially Java. Java programmers will have no problem learning C#, and for a native C# programmer, moving on to languages such as Java for Android development will be just a matter of learning simple syntax.
C# is a part of the Microsoft .NET library. At first, when .NET was introduced, C# was not as popular as its fellow .NET language, VB.NET. .NET was introduced as the next programming language after Classic ASP and Visual Basic, so most developers flocked to VB.NET as the natural next language to learn. Now, however, MVC C# is the next framework, and it’s been a catalyst for C#’s popularity. If you want to learn C#, there are a few basic frameworks, language syntax and programming style you need to know.
Object Oriented versus Linear Programming Languages
The first item you need to know is object oriented languages versus the linear execution steps of older languages. Object oriented languages aren’t a new concept. C++ is an object oriented languages. However, many of the older languages were linear. Linear languages run one page from start to finish. There are no compartmentalizing objects, which makes linear languages messier than object oriented languages. Linear languages only allow you to import code from other files, but this code does not need to have any organization or logic flow.
C# is a true object oriented language. At first, you’ll probably get frustrated. Understanding object oriented languages is difficult for most people, especially if you are used to older languages. Object oriented languages use a concept of “classes.” These classes represent parts of your code. For instance, if you have a program about a car, you map out the parts of the car as classes. You’d have a class for the engine, the interior, the exterior and maybe some classes about the dashboard and passengers. The complexity of your classes is dependent on the complexity of your car.
The flow of an object oriented language is completely different from a linear language. When a linear code file executes, the compiler runs through the code line-by-line. With object oriented classes, you call class methods, properties and events at any point in your code. When you call a method, the execution process jumps to the corresponding class and returns to the next line of execution. The C# language is written with Visual Studio, so you can step through your class code to see the flow of execution.
To Get Started
Microsoft offers the Visual Studio software for free on the company’s website. You’ll need the .NET framework installed on your computer, but if you run Windows, you probably have the framework requirements. Visual Studio installs all the necessary software to get started with C# including the C# compiler and the .NET framework if you don’t have it.
A few advantages with Visual Studio will help you get started with the language. First, Visual Studio has an excellent debugger that works with website code, web and Windows services code and class libraries. You can step through your code to more easily find bugs and errors.
Second, Visual Studio has an excellent interface that includes color-coded identification for different code elements such as classes (light blue), primitive data types (dark blue), and strings (red). Visual Studio is flexible and allows you to change these color codes, but they are well known in the industry as the default colors.
IntelliSense is probably Visual Studio’s best feature. IntelliSense tries to “guess” what you want to type next, so you don’t have to fully type out all of your code. Start typing a C# method or property and IntelliSense lets you click “Tab” to finish the syntax without fully typing the code.
Designing Applications and Understanding Classes
Classes are the main component for any C# or object oriented language. Classes are also the biggest hurdle for most people. Classes represent parts of your code, and as you display windows or views in your application, you call these parts as needed. For instance, using the car scenario, you might have a class that describes the engine. The engine can be on or off and the engine also gives the car the ability to move forward.
When you want to show the user a car moving forward, you probably need to call the engine class and its methods that move the car forward. In another window, you might want to show the car moving backwards. You would again call the engine class, except this time you’d call the method that moves the car in reverse.
This is one of C#’s advantages and all object oriented languages for that matter. You only need to create one engine class, and then this class can be called in various parts of your code. With linear code, you need to retype the same code in the execution file. With C# and object oriented code, you simply call the class and execute the parts of the class you need to represent the program’s action.
The classes you create are usually determined when you design the application. Designing applications takes some time to learn, because if you create a poor design, it can make engineering the application more difficult. You might even need to re-code several parts of your program if it is poorly designed.
The basic idea of design is to put yourself in the user’s shoes. What would you want out of the application? After you’ve figured out the basic functionality for the program, you design the classes. These classes usually entwine with your database design as well, but database design is another learning obstacle. While the classes represent your program parts, the database syncs with the classes to store the information.
For most C# programmers, a group of people determine the applications functionality, which makes it easier for the programmer. You take the functional design and turn them into classes. For instance, the functional requirements will tell you that the program is a car and the car needs to move forward, backward, make turns and turn off and on. You then take these functional requirements and use them to design your classes. In this example, you’d use the functional requirements to create methods in your engine class. The methods would represent each car action including the forward and back motion and turning the car off and on.
Building Applications with C#
You have several options when you learn C#, which makes it one of the leading languages to learn for people who plan to write a wide range of applications. Probably the most popular type of application you will eventually build is a web application. Web applications are usually written in MVC C#, but older styles such as web forms are still common.
C# is also a valuable language for writing services. Web services are applications that allow external users to call methods over the web. For instance, Twitter, Facebook and Salesforce all have web services. They are usually referred to as an API. You can write these APIs in C# and publish them to your website.
Windows services are small programs that run on servers or desktops. C# is also used to write these services that run in the machine’s background and execute code on a scheduled basis.
You can build and deploy all of these applications using Visual Studio, which compiles and publishes your app without any manual code copying or moving files to the target machine.
Learning the C# and Object Oriented Programming Style
With each job you have, you’ll be asked to follow coding guidelines. Most guidelines are universal among other development shops. The standards make it easier for other programmers to maintain your C# code after you’re finished.
To learn basic C# coding style, take note of how the syntax is formatted and presented when you watch programming videos. For instance, camel case is common for variables. Camel case is a format where the first letter is lower case, and each word following the first variable word is upper case. Classes always have upper case for each word in the class definition.
One common issue that most programmers face is understanding that C# (and any C derived language for that matter) is case sensitive. When you create a variable named “myvariable,” this is an entirely different variable from “MyVariable” or “myVariable.” If you get case sensitivity wrong, you wind up creating logic errors in your code. With Visual Studio, IntelliSense will prompt you for the correct variable syntax, which is one benefit to using C# and Visual Studio.
Understanding the .NET Framework
The hardest part about C# is learning the .NET framework. The .NET framework is a large collection of libraries provided by Microsoft when you code in the C# language. Just like a large library, you don’t know where to find certain functions, classes and code you need to complete a project, so you have to look up these parts of your code. For instance, if you want to work with .NET’s XML library, you have to find its namespace.
A namespace is a group of C# .NET libraries that encompass a group of methods. You add these namespaces to the top of your code, so you can use the library functions. There is no way to search for namespaces other than Google or experience. Experienced C# coders will remember most namespaces to add them to their code. As a student or new C# coder, you’ll probably have to use Google. You can also purchase books that give you an overview and reference for main .NET library namespaces.
The .NET framework is huge, and you aren’t expected to know them by heart, even when you code in C# for years. Most developers know that you’ll need to look up namespaces during the day, but it helps to get your feet wet with the popular libraries.
Practice Makes Perfect
While learning C# is time consuming, the best way to learn more quickly and more efficiently is to practice. If you walk away from the language and stop practicing, you’ll find yourself learning all of the basics all over again. Just like any human language, you also need to practice when perfecting a machine language.
You can accomplish practicing using a number of methods. You can program small applications from ideas you come up with. As you program them, you come across certain hurdles that you solve, and this problem-solving helps you understand the language and how to fix certain bugs.
Videos are also a great way to keep your language skills up-to-date. Videos can teach you anything from the basics to more advanced techniques. Videos are also a good option when you do walk away from the language for too long and need to brush up on the basics.
Finally, walking through steps while learning (such as Udemy.com videos) and performing the syntax on your own will help you understand how to work with C# and its libraries. You can’t just watch videos and learn a language. You need to practice. Install Visual Studio when you watch the videos and walk through the steps with the instructor. This is much more beneficial than just watching the videos.
C# is a valuable language to learn, but it’s also fun! C# also gives you an advantage when you want to learn other languages in the future such as C, C++ or Java. You can create a wide range of applications when you know how to code in C#, so you will have an invaluable asset on your resume during your job hunting.
Updated by Jennifer Marsh
Top courses in C#
C# students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.