Home Classroom C# .NET


First Code of the Series.

During our last session we got an idea about the .NET Framework and how to install it. During this session we’ll start off with our 1st coding example. This would show how easy it is to code with C#.NET. And I hope you’ll enjoy it a lot.

1. When you select “Windows Form” from “New Project” section, the following interface would appear.


Figure 1: Windows Application Form


On Right hand side a side screen could be seen, it has a Window called “Solutions Explorer” it includes all the current files related to the project we created a few minutes ago. Let’s called that project as “Digit”. Go to “Programs.cs” in that list and remove the code in it. We’ll get back to those removed code lines later. And enter the following…


Figure 2: First code of the Series


2. Now Run the Program. Now you’ll see a Message Box displayed.

Figure 3: Output Message


In the example given above,

“System” is a large Library containing pieces of code, as the developer you don’t have to type this code; you can include them just by importing the “System” package. These codes are built into the C#.

“Namespace” can be considered as two things. The first one is it acts as a collection of Libraries. And on the other hand it helps to omit naming problems in classes. It is impossible to have two classes with the same name, but they could exist in two different namespaces.

“Main” method is a very important aspect in a C# program. Execution of the program starts from there.

public – meaning that we can access the main method from everywhere in the current program.

static- meaning that it doesn’t manipulate any data.

void- meaning that the Main method is not expected to return a value.

This is a very Simple program and includes the basic features in a C# program. Feel free to move around the IDE and try out various options included. That’s the best option to get a very good idea about .NET IDE as well as C#.

Now let us concentrate on some C# programming techniques. Let me tell you that C# is an Object Oriented Language. Object Oriented Programming is a programming technique where it considers all the building blocks in programs as objects.


Understanding Objects and Properties

Let’s dive into the world of Object Oriented Programming. It is based on Objects. In more technical terms an object is a programming structure that encapsulates data and functionality as a single unit and for which the only public access is through the programming structure’s interfaces (properties, methods, and events). We’ll discuss about these terms in details later on.

In C# components like Buttons, Textboxes, etc. are considered as objects. Just like ordinary objects that we find out in the real world. Let’s consider about something real. Let’s consider about pets. Dogs and cats are definitely different entities (objects), but they both fit into the category of Pet objects. Similarly, text boxes and buttons are each unique types of objects, but they’re both considered control objects.

Objects have properties and behaviors. Properties, in another word attributes describe the nature of the object. All the objects from the same object category have the same list properties. But the property value could change from one to another. For an example dogs could have a same property, sex property, etc.

Here it is in more descriptive manner.


Figure 4: Properties of a Dog Object


In programming these property values are hidden. But they could be retrieved by using Getter and Setter matters. Here is how we can set property values.

{Object name}. {Property name}

This could be equal to the value that we set. Ex: The Text property of a Textbox (txtName) can be changed by this. txtName.Text = “Address”;

But fortunately in C#.NET we don’t have to code these values to each and every object. Values of a certain object could be changed using the Properties Window

Figure 5: Properties Window for a Windows Form


Understanding Methods

In addition to Properties Objects have Methods. Unlike properties ,methods (Behaviors) can perform certain tasks. Let’s discuss about this using more simpler example.

Figure 6: Methods of a Dog Object


Methods too could be triggered using the same way which is used to call Properties.

{Object Name}. {Methods Name}

Example: Eat method of a Dog can be called like this way. For a dog called ‘rovy’

rovy.eat ();

Let’s look at a sample code to see this in a more clear view. This would create a small and the data which is entered would be displayed in a Message box.

1. Design Data Entry form using C#.NET. The output you get is seeing below. By double clicking on the button or Pressing the Events option from ‘Solutions Explorer’, you can get the click event of the button.


Figure 7: Sample Data Entry Form and Output

The code related to above example is here.

2. The code related to above example is here.

In this the import statements (System namespaces) are not included; they’ll be added by the IDE, while you are typing the code. This code section is to understand the Methods and Properties. Furthermore some concepts here are not yet covered during this series. But later on we will put our attention into them as well. But for now let’s concentrate on these areas.

This code segment starts with the class statement. Class is a very important section in any programming language. Class is the blueprint of objects, we’ll be discussing about classes in detail later on. On the code next comes the constructor, it has the same name as the class, but with the exception of certain things. This too needs to be discussed in detail.

Next it is the method invoked for clicking the button. The method name is ‘btnView_Click’. This method consists all the action which needs to be triggered when the button is clicked. As you can see when the button is clicked two arguments are sent to the method. Method is a piece of code segment which could be used to call another set code in the program.

Methods are much like events, but rather than being executed by a user interacting with a form or control, methods are executed when called by a code statement. Two types of code procedures are used in Visual C#:

  • Methods that return a value
  • Methods that do not return a value

This method has ‘void’ as its return type, which says that this method doesn’t return a data type. Apart from this a method could contain any variable type as its return type. The content in side a method are surrounded by two curly brackets (‘{}’). When there is a return type keyword ‘return’ must be used with the variable name that you going to return. This should be added at the end of the method. If not the compiler will display as error message.

Method calls in Visual C# must always have parentheses. Sometimes they’ll be empty, but at other times they’ll contain data to pass to the method. Methods are generally used to perform an action using an object, such as saving or deleting a record in a database.

Variables are temporary memory locations which are used to store values. Variables have lot of types, these could be used depending on the type of data that you need to store. In here we have used ‘string’ as the data type and we have used three string variables for this example. What we have done is that the value we got from the (get method) from the textbox property is sent to a string variable and then it is stored there for future reference, in our case to be displayed in the Message box.

‘name = txtName.Text;’ In here ‘Text’ property of the textbox, the value which was entered by the user is assigned to the variable name. Same thing is done with the other textbox prroperties as well. This is jut a small demonstration, you can can go trought eh ‘properties Window’ of a Textbox and findout about all the available properties. Properties, on the other hand, are used to get and set attributes of the object. One way to tell in code whether a statement is a property reference or method call is that a method call will have a set of parentheses after it.

Same could be done with other GUI objects like Buttons, Labels, Combo boxes,etc.. Next those values which were stored in variable would be displayed in a Message Box. The escape Characters (‘\n’) which were used here are used to indicate that the next variable value should be displayed in a new line. When the user runs the program, enters the values and presses the ‘View’ button the details which were entered in the textboxes would be displayed in Message box. This example was used to clarify the use of properties of objects in this case Textbox objects and methods of objects (the click method of the button is evoked).

This is a very simple example, but these are the basic components and concepts used in almost all the complex systems. So it is vital to have a pretty good understanding about objects their properties and methods that they contain. In our next session we’ll start to concentrate on some really important aspects in C#. Those are variables. Till then have a nice time.



Introduction to C#

Learning a new programming language could be an intimidating factor, especially if you are new to programming. Some people think that coding a program and the output it brings is magic. But the actual story is that it is an art and piece of science. The way to become a successful programmer and a keen programming enthusiast is to learn things in step wise. It is essential to get to know the basics of any programming language if you want to be a successful programmer on it. When you get addicted to programming it will be fun and will start to enjoy it a lot. In this series we will be concentrating on C# and how to create nice applications from it. You will understand that C# is quite easy to follow and it is enjoyable.

C# is a programming language which developers can use to develop Windows based applications. As you have experienced with other programming languages there is no need to install separate compliers in C#. Furthermore when running the code, manual compilation is not required. The reason for that is that C# comes with the .NET framework developed by Microsoft Corporation. So as a user what you really want to do is to install .NET framework in your machine, with that all the components which you need to develop (code, compile and run) C# programs would be installed. We’ll see how we can install C#.NET in your computer later in this session. Before that let’s gather some information about the history and evolution of the .NET framework.


About C#

Before diving deeper into the pool of C#.NET, let’s get a small idea about the history of it and how C#.NET became the popular piece of technology as it is now.

In and around 1997 Microsoft started the work on .NET. By that time the .NET framework was called “codename Project 42″ and “project Lightning”. During that time Microsoft was starting to face some problems, they were criticized about Visual Basic; they lost ground against Java and C++ and at the same time Sun Microsystems plus the US government open cases against Microsoft in charges of monopoly and copyrights. To overcome these issues Microsoft came up with C#. Since then .NET had improved with leaps and bounds. And comprises with many languages and technologies like ASP, Visual C++ and J#. The name “C#”(C sharp) came from the musical term “#”. In musical language the sign “#” means: A semi-tone over the note. The intention behind that original name was to be shown that C# is a little bit better than C++, just like C++ used the “++” sign to express its power over C.

However, apart of its metaphysical “power” C# really differs from C++ and other languages. And it’s without a doubt one of the best languages nowadays. And many developers are using C# to develop Windows based applications over languages like Java, C++, etc…

All these languages have characteristics of their own and the usefulness of each language changes from the applications which it is used. But C# and addition of .NET Framework has brought an extra dimension to C# and it has immensely helped to increase its popularity.

Here are some reasons why C# outscores some of its counterparts like C++ and Java.

  • C# comprises of four paradigms, those are: functional, imperative, generic, and object-oriented (class-based). For comparison Java has only 3 paradigms while PHP and C++ only 2 paradigms.
  • Languages like C++ and java didn’t have an advance IDE like what C# has. Now Java has Netbeans.
  • C# is better in some other ways as well, Visual Studio comprises of a ‘Garbage Collector’ and unlike in C++, pointers (Very Helpful, but complicating at times) are removed.
  • C# is Object oriented, so classes are used in building programs (Which will be discussed later in detail).
  • ASP.NET is also a part of .NET Framework, so C# programs can be easily embedded with Web pages designed from ASP.

C#.NET has gradually evolved to the current state from its beginning. As mentioned above learning C# is easy. To start doing it you only need a single program called Microsoft Visual Studio. All the required components which you need to code, compile, run and debug C# code is embedded with it.

Thus far Visual Studio has come with versions like, Visual Studio 2003, Visual Studio 2005 and Visual Studio 2008. These get a Visual Studio copy as a commercial edition or as a free edition. But this tutorial series would be using the free edition of the Visual Studio which is the Visual Studio 2008 Express Edition. If necessary you can always go for a commercial edition of Visual Studio even.


Setting up the Computer for C#

In case if you don’t have a copy of Visual Studio Express Edition you can download it from here.

  1. Visual C# is usually presented with as a ‘Green Box’
  2. You can either save the installer into your machine or download it directly.
    Here’s a brief description about the installation process.

This is the screen which appears at the beginning, click next and move to the next one

Figure: 1_1


Accept the Agreement statement and move to the Next step.

Figure: 1_2


Select the version you want to install and move to the Next step

Figure: 1_3 – Select the Download Version


Browse the location in your machine to install.

Figure: 1_4 – Select Install location


Then the installation process would continue.



After the installation process you would be asked to restart the machine. Do it as it says.

Now you are ready start learning C#.NET.


Selecting a New Project for C# in .NET
  • After you have started the Visual studio, open new project. On the main menu click File/New project.
  • Now on the window that appears, choose on : “Windows forms application” and Click ‘Ok’


Figure: 1_6: Starting Screen of c#.net


  • Then a screen similar to this would appear.
Figure: 1_7 – Main Interface Screen

To get a broader view and understanding about C# and Microsoft Visual Studio you can visit http://www.learnvisualstudio.net/ and download video tutorials.

Now you can start coding your First C# program. But we will start doing that in our next session.
Till then have a nice time. Thank you…