Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, November 24, 2011

Learn C# Programming Tutorial Lesson 9 - Inheritance, Abstract Classes and Interfaces


Inheritance

Inheritance is the ability of classes to inherit things like methods and variables from another class. This means that you can create one parent class that has a method in it and all the classes that inherit from the parent class can use that method as if it belongs to them as well.
The best way to explain inheritance is with an example. First we will create a class called ParentClass which has a method in it called SayHello() which prints "Hello" on the screen.
using System;

class ParentClass
{
   public void SayHello()
   {
      Console.WriteLine("Hello");
   }
}

Learn C# Programming Tutorial Lesson 8 - Classes and Objects


What is a class?

A class is basically a single module of a program. All C# programs other than the most basic ones use many classes. Classes help to divide up a program into modules so that it is easier to work with the code.
Something you must understand about classes is that they are actually like an architect's plan for a house. You don't usually use the code in a class directly but rather you create objects from a class and use the objects. An object is like the house that is built from the architect's plan.

Learn C# Programming Tutorial Lesson 7 - Working with Strings


Strings are used when displaying messages to the user which happens in almost every program which means that it is very important to understand them well. This lesson will show you some ways to work with strings and how to make it easier to use them.

Methods built into the string data type

The string data type has many methods built into it. The ToUpper() and ToLower() methods for example will change a string to upper case and lower case letters respectively.
string s = "Hello";
string upper = s.ToUpper();
Console.WriteLine(upper);
string lower = s.ToLower();

Learn C# Programming Tutorial Lesson 6 - Methods


When you want to repeat the same lines of code in many different parts of a program you should put that code inside a method and just call the method as many times as you need to.
A method must be created outside other methods including the Main method. Here is an example of how to create a method called PrintHello which will print hello on the screen. The whole program is included to show you where the method must be created.
using System;

class methods
{
   private static void PrintHello()
   {
      Console.WriteLine("Hello");
   }

   public static void Main()
   {
      PrintHello();
   }
}

Learn C# Programming Tutorial Lesson 5 - Arrays


Imagine that you want to enter 10 numbers and calculate their average. You would need to declare 10 variables to do this. That is a lot of variables to have to declare and you still have to read the values in one at a time for each one. An easier way of doing this is to use an array. An array is many variables grouped together under one name.
To declare an array you must first have the data type followed immediately by empty square brackets. After that you put the array's name. Here is how to declare an array of integers called arr.
int[] arr;
This is not an array yet but just a reference to an array. You must set the number of dimensions using the new keyword followed by the data type again with square brackets after it that contain the number of elements you want in the array. The following example creates an array with 3 elements.
int[] arr = new int[3];

Learn C# Programming Tutorial Lesson 4 - Loops


A loop is something that is used to repeat a section of code as many times as you want. If you wanted to print Hello 100 times then you could use 100 lines of code or you can do the same in only a few lines using a loop.

For Loop

The for loop counts from one number to another number. You start a for loop with the for keyword which is followed by brackets. The first section inside the brackets is where you set the starting value of the loop counter variable. The second section is the loop end condition. The third is where the loop counter is incremented. The code to run for the loop goes after the brackets.

Learn C# Programming Tutorial Lesson 3 - Decisions


if statement

The if statement is used when you want to make a decision in your program. Here is an example of how you would check if the user enters the correct password.
Console.Write("Enter password: ");
string password;
password = Console.ReadLine();
if (password == "abc")
   Console.Write("The password is correct");

Learn C# Programming Tutorial Lesson 2 - Variables and Constants

What is a variable?

A variable is a block in memory that you can use in a program to store data. You can store different types of data such as numbers and words.

Using variables

Before you can use a variable you must declare it. To declare a variable you must first choose what data type it is. If you want to store a number then you must use the int data type. You must then choose a name for the variable. We will use the name MyInt in the following example.
int MyInt;

Learn C# Programming Tutorial Lesson 1 - Your First Program

Requirements

To be able to do this tutorial you will need to have a good understanding of how to use a computer. You will also need to either get Microsoft Visual Studio.Net or download the free C# compiler from Microsoft's website.

Writing the code

You have 2 choices when you write a C# program and they are to use the Visual Studio IDE to write your code or to use notepad. We will be using notepad. You can start by typing the following line in notepad.
Setec Institute || Group : SH6 | | Name : Kim Chanthy. Powered by Blogger.
 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hot Sonakshi Sinha, Car Price in India