PROWAREtech

articles » current » dot-net » tutorial » c-sharp » page-1

.NET C# Tutorial - A Beginner's Guide - Page 1

Introduction: Object Oriented Programming (Encapsulation, Polymorphism, Inheritance), Comments, Code Blocks.

While this site tries to incorporate both C# and VB.NET code, C# is the overwhelming favorite. As such, this guide is designed to help make the code on this site more understandable.

Introduction

C# is influenced by C/C++, which also influenced the development of Java. Java introduced program portability to the world but it lacked cross-language interoperability also known as mixed-language programming. Cross-language interoperability allows creating large, distributed software systems. C# was created with cross-language interoperability in mind.

Object Oriented Programming

Object-oriented programming is at the center of C# and all C# programs are to at least some extent object-oriented.

Encapsulation

C#'s basic use of encapsulation is the class, which defines the form of an object. A class specifies both the data and the code that will operate on that data.

Polymorphism

Polymorphism is fully supported in C# via interfaces, abstract classes and methods, overridden methods and virtual methods.

Inheritance

Inheritance is fully supported in the class declaration.

A Simple Program

It is possible to create a program without an IDE (integrated development environment) like Microsoft's Visual Studio, however, because there are fully capable free versions of Visual Studio available then this tutorial will assume that it has been downloaded and installed on a Windows machine.

From within Visual Studio, create a new C# Console Application as pictured here.

There should be some functioning program code that looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
		}
	}
}

Modify the code to look like this:

using System;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			// this is a comment
			int x = 1000;
			Console.WriteLine("The variable x = " + x);
		}
	}
}

Hit Ctrl+F5 on the keyboard to run the program. This should be displayed:

Some of this code should be pretty self explanatory. The text "The variable x = " is a string literal. Console is a class responsible for console input and output (I/O) and it is part of the System namespace. using System; means that the program code is using the System namespace. namespace is covered later by this guide. int x = 1000; is an integer variable being declared and initialized with the value of 1000. WriteLine() is a method that writes a line of text to the console window. Notice that each statement is ended with a semi-colon. This is important - don't forget it.

Comments in Code

To comment a single line, use //. To comment several lines, encase the lines in /* and */.

// this is a single line comment

/*
this is a comment
taking up several
lines of code
*/

The Code Block

The code block is usually a group of two statements enclosed in between opening and closing curly braces. Though a code block can have only one statement. The code inside the Main() method is a code block.

<<<[Page 1 of 7]>>>

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE