An Introduction to C# Programming Language
History of C#
Originally, Microsoft developed and deployed the ASP (Active Server Pages) language. This language is now called Classic ASP, and its support has been discontinued. ASP let programmers create VB-like scripts to develop dynamic web pages. These web pages ran on older Internet Information Services (IIS) servers running Windows. Some Linux providers also supported Classic ASP, but it was usually a non-standard way to create web pages.
In the late 1990s, Microsoft decided to create a language that would compete with Java. During this time, Java was the most common language, and its popularity was rising. Classic ASP had its place, but the software needed to create the product was a paid application. Java was used to create web applications, and it was preferred among developers who understood C and C++.
In 2000, Microsoft released its first version of C# as a part of the .NET framework. VB.NET was also an option, but VB.NET followed the standard Microsoft Visual Basic syntax while C# was similar to Java, which is also a C-style language.
Initially, C# was not as popular as VB.NET. Since many Windows desktop applications were written in Visual Basic and web applications were written in Classic ASP, the language of choice for developers at the time was VB.NET. VB.NET was an easier language to migrate to when changing to the newer framework. However, the language of choice started to shift as more people realized the similarities between Java, C and C#.
The name given to C# was originally "Cool." Microsoft knew that it was working with a language very close to C++, so they wanted to indicate that its new language was one step above C++. Instead of naming the new language C+++, they decided to go with the name C# (pronounced C sharp), which references the musical notation that a note should be higher in pitch.
Through the years, C# finally gained traction as a preferred Microsoft language. What makes C# different than most popular coding languages on the web is that the code is closed-source. Open-source coders claim that C# and other closed source languages are too difficult to work with, but closed source has its advantages.
First, working with any Microsoft platforms is much easier with the company's own language and frameworks. You could run open-source languages such as PHP or even Java on Microsoft web servers, but it's much more cumbersome and includes more overhead than just using Microsoft's natural language used in Windows applications. C# also works well with Microsoft's SQL Server database backend. In most companies that run C#, the database choice is Microsoft SQL Server. Both the language and the database work seamlessly together. You can also work with open-source database solutions with C#, but it's more common to keep the platforms related when working with high-end enterprise solutions.
Second, closed source applications could be considered more secure. If a hacker is able to gain access to a web server with open-source coding, the hacker now has access to the entire source code for the application. With C#, the language is compiled and DLLs stored on the web server. If a hacker gains access to the web server, he only has compiled binary files. This still isn't a guarantee that the hacker can gain access to the source code. There are plenty of decompilers on the market, but it makes it much harder for a hacker to view the source code. The C# language even lets you encrypt the password stored in the site's web.config file, which is where many of the sites settings are found.
Another advantage of C# is that these sites are not the main target for hackers. Hackers prefer out-of-the-box solutions such as WordPress and Joomla. These sites become targets, and are much more easily scanned and breached.
Finally, C# works well with the Visual Studio environment. Microsoft assumes that its code is used by small and large application design shops. With larger shops, the need for collaboration tools requires an environment that allows coders to check in, check out, merge, and roll back code. The Visual Studio environment along with Team Foundation server gives developers this option.
C# and ASP.NET in general have gone through a different style of coding. The original framework required developers to work with web forms. Web forms are a framework that lets the developer control all aspects and events of a page including when the page loaded, the way elements rendered on the page, and when the page stopped loading. Developers could also change the habits of a page that was unloaded from the server. These web forms remained the standard style of .NET programming until a few years ago.
In 2009, Microsoft released an entirely new framework from which developers could create web applications. The MVC (model-view-controller) pattern is a way for coders to create web applications without the cumbersome way web forms required coders to control the way pages load. MVC was a complete overhaul to the way coders were used to creating C# applications.
With MVC, the developer does not need to worry about the way a page loads or how it unloads. Elements are not controlled through events, and they don't need rendering from the standard web form interface. Instead, coders created models, views, and controllers. A model represents the way a data object is laid out. A view is the HTML page (named CSHTML in MVC) displays on the browser. The controller contains the business logic and code workflow for the frontend design.
The MVC pattern of application programming is the current way developers work with applications. Web forms are still found in some older, legacy applications, but you won't see them as much as MVC. MVC is considered the most powerful version of the .NET framework. It's also considered a much more easily coded application. MVC also has a stronger foundation on object-oriented programming (OOP). Other languages use the MVC pattern, so it's useful to learn it to ensure that your skills can span several areas of development.
Installation and Setup of the C# Environment
You need two main applications to build C# applications, but a third is also required for database backend storage. Since you'll probably need a database in your application, we'll cover the three main components to get started with C# programming.
The first component is Visual Studio. Visual Studio is the main IDE for any Microsoft development. You can even work with SQL statements within Visual Studio, although Management Studio is preferred.
Visual Studio Express can be downloaded for free off of Microsoft's website. Visual Studio is separated into desktop development IDEs and web development. We'll focus on the web development download, but C# syntax is the same throughout each version. You should download the version that corresponds with what you plan to do. If you plan to code web applications, download Visual Studio for Web. If you plan to create desktop applications, download Visual Studio for Desktop. Cloud applications are becoming more and more dominant over desktop applications, so consider turning your app into a cloud application if you plan to code desktop software.
The Visual Studio installation process takes several minutes. It can take up to an hour if your desktop isn't up-to-date with the latest, fastest software. You might ask if you need Visual Studio. Other programming languages let you use a variety of IDEs, but C# is different since it's proprietary closed source programming language. You need Visual Studio, so there is no avoiding the software.
Once you install the software, open it for the first time (your computer will need to reboot before you can open the software). Visual Studio asks you which environment you prefer when you open it for the first time on your computer. You have the choice of several different programming languages and frameworks. Choose C#, and this environment layout will open each time you open Visual Studio and create a new project. It's not a critical step, but it will save you time when you need to create a new project.
The next component you need is Internet Information Services (IIS). IIS is included in all Windows desktop and server versions, but it's not obtainable if you run Home Edition on your desktop. You'll need to upgrade to Professional versions of the operating system to gain access to IIS. IIS is used to configure the website, so you can run a test version of your website after it's compiled.
Visual Studio includes an express version of IIS that you can use to view an application in a browser. However, you won't be able to compile and test your software for deployment. For developers, having a Professional version of Windows is crucial for success. You can get away with just using the Visual Studio Express version of IIS while you learn, but to take your programs any further than development, you'll need a full IIS version. If you have a host provider, you can create a test server with your host. Of course, you need Windows hosting to go with this testing option.
Whether you work with your local computer or a host provider, just know that the code must be compiled before you release it to an official web server. You can run uncompiled in older web applications, but MVC requires the DLLs to run on your server. You might ask why you need to test code on a test server before deploying it to a production server. Shouldn't the application work if it works on your desktop? This is a mistake that many new developers make.
First, the development environment is always different than the production environment. This means that hardware, software, networking components, and machine settings are different than what is on your local computer. These differences may not seem important, but they can create run-time bugs in your code that you won't catch on your development platform.
The third component is SQL Server. You can use any database application with .NET as long as the database provider includes drivers for the Windows and .NET environment. This means you can choose an open-source platform such as MySQL. Oracle is another database used commonly with .NET applications.
Although you can use third-party database solutions, it's best to use Microsoft SQL Server since it integrates seamlessly into your applications. You can view your database design and data directly in the Visual Studio browser. Microsoft also offers a SQL Server Express version. This version is free, but you cannot use it with a full production application. SQL Server Express is basically the SQL Server database environment without the power to work fully with applications once they begin to drive traffic.
You can download Microsoft SQL Server Express from Microsoft's official site. Microsoft SQL Server is an expensive database server backend, so you'll need a large investment if you want to download the full version. Your web host should also offer a SQL Server database solution. You can use the host SQL Server as a way to test your applications on a production server, but don't ever use the production database as a way to test your applications. You run the risk of accidentally deleting data, corrupting data, and at the very worst crash your production environment.
Once you install SQL Server Express, ensure that you create a new user for the database server. Choose the "Mixed Mode" login option. Mixed Mode lets you use SQL Server specific credentials, which means you don't use the local Windows account to log in to the SQL Server. Mixed Mode is more secure, because a hacker is still unable to access the database even if he gains access to the server's user name and password credentials.
With Mixed Mode, you'll install SQL Server Express, and the software will ask you to enter a password for the default system administrator account – sa. SA is the highest level account on the database server, so you want to ensure that the password is secure. You also shouldn't ever use SA to work with your applications. SA shouldn't be used in the web.config or in any area of your C# code. If the hacker is able to gain access to the SA password, he has complete access to your database server, which can be devastating for a business.
We mentioned host providers, and you should look into one while you learn C# if your intention is to work with a web application. Many of the code changes you make can be uploaded to a web server, so you can see these changes on a production server while you learn. Part of understanding code is deploying your code to production, which is a task in and of itself. It's not always difficult, but you'll learn how to troubleshoot and debug code when deployments fail. Usually, initial release of a new application has a few bugs related to the environment and web server configurations.
If you decide to order a hosting solution, it should be at least Windows 2012 to support the latest versions of ASP.NET. Windows 2008 is still viable, but soon this server operating system will be two generations behind the latest Microsoft Server operating system. Microsoft tends to discontinue support for older operating systems, and it's not easy to upgrade without causing issues with your application.
One more issue with Visual Studio you should remember – Visual Studio has a new release almost each year. Microsoft has versions 2012, 2013, 2014 and now 2015. You need certain .NET versions for each Visual Studio version. The best way to get started with C# is to download the latest Visual Studio version. This version will install the latest .NET framework on your computer. Again, you also need this version on your web server, so you could have issues if you have an older web hosting server. In other words, all versions of Visual Studio, the .NET framework, and your web server should match. If any one of these doesn't have the version that you code your application in, you could have errors when you deploy the application.
C# is a great language to learn if you want to build Windows applications. Windows runs other open-source language and framework, but knowing C# will let you create integrated applications that run much more smoothly on a Windows machine. We'll guide you from the basics in C# structure and syntax to the more complex concepts of object-oriented programming and design.
- Understanding and Applying Enums Concepts in C# Programming
- Components of Process in C# Programming: Threading
- Object-Oriented Concepts Inheritance and Polymorphism in C# Programming
- Using C# Strings in C# Programming
- Using Operators in C# Programming
- CSS Explained in Layman's Terms
- How to Do Advanced Queries in MySQL
- An Introduction to OS X Yosemite
- Using the INSERT Statement in SQL
- What are Inductor Circuits?
- User Agent Values in jQuery
- How to Restore and Backup MySQL Databases
- Using the DELETE Statement in SQL
- Benefits and Setting Up MySQL Replication