[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
LoadingDynamicLibraryC » startpage » CSharp Tutorial
This article will try to be what may be called a Beginners Tutorial. However it's rather an introduction into .NET in general with an emphasis on C#.
C# itself is the successor of C and C++ and supports all common object oriented programming features and concepts as they are used in C++ or Java. One of the main differences of C# to C is, that C# doesnt support pointers. All values are assigned by Reference, like in Visual Basic. Another difference to C is that all programs are represented by classes. Consider the following example:
This concept is similar to that of Java where all programs are represented by classes too. Another, third difference to C and C++ is, that you dont need to use the #include <> directive to use library files. In C#, the compiler generates the necessary includes automatically. For example, if you write a Windows Forms application the compiler will generate the following code:
In the HelloWorld console version above I have used the Console.WriteLine()
method to print <i>"Hello World"</i> on the screen. All function calls in C# are methods.
The Console.WriteLine() function is a method because its implemented in the
class System.Console. Any function implemented in a class or struct
is called a method. Having said this, i shall point out that it is not possible to write functions
independent or outside of classes.
Before we dive deeper into C# and .NET programming, I shall first give an
overview of the syntax of C#. C# is a high level programming language that
supports all common features of its predecessors like C++ and C.
Variables are used to store values. In C#, variables refer to a certain range in
memory space. Another important aspect of variables in C# is, that C# is a so
called type-safe language. That means that all variables must be first declared
and then defined with a certain class of data type. Consider the following code
snippet:
[/code]
Besides these primary data types, C# supports also so called derived data types as shown in the table below:
Now I will modify the Hello World console program in a way to read input data
from console and print this data on screen together with the Hello World
message.
In this version of HelloWorld we first ask the user for his name and then greet him with Hallo plus his name. This is just a more solid variation of the Hello World program. This version also shows another thing which is specific to the C# deals with variables. If you want to display variable values in a string, you need to use the { } braces with a number included in them, starting by 0 to tell the compiler that the braces replace the formatting string. The following code snippet shows this in detail:
I will use the basic Hello World program to show you some basic programming concepts of C#.
In this version we greet the user by using the Environment.Username attribute of the Environment class. The Enviromenment class contains information about the machine, the user, etc. The last line shows the user the system time. We get this information asking the system for the current time, which is represented by the Now attribute of the DateTime class.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
LoadingDynamicLibraryC » startpage » CSharp Tutorial
Getting Started with C# .NET
This article will try to be what may be called a Beginners Tutorial. However it's rather an introduction into .NET in general with an emphasis on C#.C# itself is the successor of C and C++ and supports all common object oriented programming features and concepts as they are used in C++ or Java. One of the main differences of C# to C is, that C# doesnt support pointers. All values are assigned by Reference, like in Visual Basic. Another difference to C is that all programs are represented by classes. Consider the following example:
//A Hello World console program
using class System;
public class HelloConsole
{
[STAThread]
static void Main(string args[])
{
System.Console.WriteLine("Hello World");
}
}
This concept is similar to that of Java where all programs are represented by classes too. Another, third difference to C and C++ is, that you dont need to use the #include <> directive to use library files. In C#, the compiler generates the necessary includes automatically. For example, if you write a Windows Forms application the compiler will generate the following code:
using System.Windows.Forms; //Use Windows.Forms classes using System.Windows.Forms.Drawing; //Use Windows.Forms.Drwing classes public System.Windows.Forms.Form1 { //Compiler generated constructions Form1() { InitializeComponent(); //Initialize Components of Windows Form } }
First Steps in C# .NET
In the HelloWorld console version above I have used the Console.WriteLine()
method to print <i>"Hello World"</i> on the screen. All function calls in C# are methods.
The Console.WriteLine() function is a method because its implemented in the
class System.Console. Any function implemented in a class or struct
is called a method. Having said this, i shall point out that it is not possible to write functions
independent or outside of classes.C# Syntax Basics
Before we dive deeper into C# and .NET programming, I shall first give an
overview of the syntax of C#. C# is a high level programming language that
supports all common features of its predecessors like C++ and C.Variables & Data Types
Variables are used to store values. In C#, variables refer to a certain range in
memory space. Another important aspect of variables in C# is, that C# is a so
called type-safe language. That means that all variables must be first declared
and then defined with a certain class of data type. Consider the following code
snippet:int i = 0; //variable of type int, defined with a value of 0 string astring; //declaration of a string variable, no value defined double k = 3.1457; //definition of a double variableAs you can see from the examples above, there is a difference between the definition and the <strong>declaration</strong> of a variable. The declaration is simply the introduction of a variable to the compiler. The definition is the assignment of a value to a certain variable of a specific data type
[/code]
int i; //declaration only <br /> int i = 0; //definition of i with the value 0 <br />
Data Types of C#
C# supports the following data types: Data Type Example int 1,2,3 etc. short 1,2,3,-5,-6-,-7 long 1,2,3,4,-5,-6,-8 double 3.1,2.4,5.6,1,2,3 float 3.1,4.5,6.8,1,10, char 'a','b','c' String "hallo","alexander","May"
Besides these primary data types, C# supports also so called derived data types as shown in the table below:
Data Type Example DateTime 10.01.2004, 01.05.,"12.03.01"
Doing something Useful
Now I will modify the Hello World console program in a way to read input data
from console and print this data on screen together with the Hello World
message.
using System.Console;
class HalloWorld2
{
public void Main(string args[])
{
String name;
Console.WriteLine("Enter your name: ");
name = Console.ReadLine();
Console.WriteLine("Hello {0}" + name);
}
}
In this version of HelloWorld we first ask the user for his name and then greet him with Hallo plus his name. This is just a more solid variation of the Hello World program. This version also shows another thing which is specific to the C# deals with variables. If you want to display variable values in a string, you need to use the { } braces with a number included in them, starting by 0 to tell the compiler that the braces replace the formatting string. The following code snippet shows this in detail:
string name = "alexander";
string secondname = "fabian";
string thirdname = "thomas";
Console.WriteLine("Hello {0}" + name);
Console.WriteLine("{1}" + secondname);
Console.WriteLine("{2}" + thirdname);
I will use the basic Hello World program to show you some basic programming concepts of C#.
Showing some System Information to the User
using System.Console;
class HelloConsole3
{
[STATthread]
public void Main(string args[])
{
Console.WriteLine("Hello {0}" + Environment.Username);
Console.WriteLine("You are working on {0}" + Environment.Machinename);
Console.WriteLine("Its now {0}" + DateTime.Now.ToShortString());
}
}
In this version we greet the user by using the Environment.Username attribute of the Environment class. The Enviromenment class contains information about the machine, the user, etc. The last line shows the user the system time. We get this information asking the system for the current time, which is represented by the Now attribute of the DateTime class.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
