Imports System
Public Shared Sub Main()
Console.WriteLine("Hello World!"
End Sub
Archive for January, 2008
.method public static void Main() cil managed
{
.entrypoint
.maxstack 8
ldstr "Hello world!."
call void [mscorlib]System.Console::WriteLine(string)
ret
}
I was looking for something interesting to read in the MSDN Library and found an article about Pointer types in C#. Yes, they really exist! I didn’t know that before.
Pointer types in C# can only be declared within unsafe-context (e.g. methods declared as unsafe or inside an unsafe-block). It must also be compiled as unsafe code (/unsafe in command line).
unsafe static void PointyMethod()
{
int e = 10;
int *p = &e;
Console.WriteLine(”{0} = {1}”, (int)p, (int)*p); //Write the address and the value
}
Remember! You can only point to basic types like int, char and double, not to user-defined types like strings and arrays. These are classes.However, you are also much more limited than in C because of the memory management integrated in the runtime. So you cannot get as much control you need or want. The only use of pointers is when you call a DLL, else it is recommended to use reference types.
Algorithm implementations
Published January 13, 2008 Code Snippets , Links , Programming 0 CommentsTags: Algorithms, Programming
I accidentally found this on Wikibooks. Something for mathfreaks and computer nerds like me :).
Bill Gates is retiring, or not?
Published January 12, 2008 Humor , Links 0 CommentsTags: Bill Gates, Microsoft
Have you ever wondered what Bill Gates is going to do now when he has left Microsoft?
Here’s the answer!
http://www.istartedsomething.com/20080107/bill-gates-last-day-microsoft-video/
The world of procedural programming is huge and sometimes complicated. It’s quite difficult to think in that way. You got a function or procedure that you must call from another method and suddenly you’re lost among lines that you don’t remember anymore. What can you do to prevent this from happening? There is one solution, it’s called Object-Orientated programming (OOP). In the world of OOP you’ll be introduced to some new stuff that will make your code much more readable and also let you think in a more natural way. In OOP you store your related methods (functions and procedures) in classes. Things is much easier if you have all related code on the same place. You can still call your methods like in traditional programming, but only if they are static. They are only part of a class.
class AClass
{
public static void Function(int x)
{
return x * 2;
}
}
AClass.Function(2); //Calling a static method
Why classes? Why don’t just create libraries like in C and place code in separate files? It’s ok, you can do that and you still can if your compiler enables that. But how many files of code do you want to have? 5 or 500? You will see that you can do so much more with classes. Well, it’s time to move on to the really cool stuff.Look around! What do you see? Things like your chair, a pencil or even your computer? They are all objects. Everything are objects, even you. No, what is this? Are we practicing some kind of religion? No, we’re going to use this way of thinking in our programming. We will now use the very usefull classes again.
Imagine that you’ve got a house, and it’s unique but still a house. Everyhouse has roof, windows, door and so on. But they may be distinguished by the number or types of those. We could say that your house is an object of the type House. You could also say that you got a blueprint (or a template) that your house was made after. Like cookies baked from a recipe, the type. Those types are classes. Do you get it?
class House
{
/* Some field variables */
private string field;
public House()
{
//Constructor
}
/* Some methods that are either specific for the
object or static (you don't need to create an object) */
}
You can create an instance of this class just like you do with integers and chars. (In Java and C# you use the new keyword when you create instances of user-defined classes.) House myHouse = new House();
Now you got a variable of the type House. You can then use the methods within this object to modify it’s properties (field variables) like so:
myHouse.OpenDoor();
Remember that non-static methods can only be reached through an instance. And don’t forget that methods still can take parameters and return values.
You can do the same thing with structures (usually called structs) in C#. Both are user-defined types, but there a some differences though. Structs are value types in contrast to classes which are reference types. In short you can say that a reference type variable holds a reference to the object on a memory called the heap. Value type variables are the values. Those are stored on the stack. Both memories are named after how they work.
Another cool thing is inheritance which allows classes to inherit fields, properties and methods from another, so called base class. But we won’t discuss this in this post.
The conclusion is that it’s a new way of thinking that makes programming much easier. Many modern programming languages are OOP-languages: Java, VB.NET, C#, C++ and Delphi (Object Pascal). So if you’re planning to migrate to any of those languages you may need to understand this. You who are new to OOP-programming may think that this is hard but don’t give up.
Good luck with your programming!
Notes:
-
Most of the code written here are compatible with C# and Java. Because both of those languages are derived from C through C++. But many of the examples will not work because they are incomplete.
This is my own personal thoughts and I just want to make things simpler for you.
Beginning with programming
Published January 12, 2008 Programming , Tutorials 0 CommentsTags: Programming
Are you a beginner or do you want to learn how to program your own applications?
You need to know the basics to master the art of programming. Don’t worry I’ll teach you. First I will introduce some terms which are important to know. You may have heard many of following words when you practiced mathematics or studied languages. Don’t be confused. It’s really not that complicated even though there are some differences.
Let’s start
Identfiers are names for the elements (variables and keywords) of your code.
Keywords are reserved words that are used by the language and thereby not allowed when naming your variables. There are keywords that are aliases for build-in types.
Variables are used as in mathematics. They hold a value of a special type. You may change this value during runtime (when running your program) as you do in mathematics. Most of the program languages have variables bound to a special type. So you can’t change the type of variables.
int MyVariable = 2; //A variable (MyVariable) for integers which holds the value 2;
Types. If you know mathematics you probably know sets of different types of numbers (Z=Integers, N=Naturals etc.).
Well, types are like sets but still not. You declare a variable of a type and it can hold just the value that the type allows.
Some important types are integer, double, float and character.
Statements are equivalent to mathematical expressions or linguistical sentences. (Note this tutorial is written in C-like syntax and “;” is the statements terminator.)
int val = 2 * 3; //Multiply 2 with 3 and initate the variable val with the result.
Some pre-made statements are “built in” in certain programming langauges. Most notably the usefull if and while statements.
Functions are code blocks which returns a value. It’s just like in mathematics. Of course you can send one or many values (paramters) into a function.
Procedures work like functions but doesn’t return a value.
Method is a word which is refering to both functions and procedures because of their similarities.
That’s all for now ![]()
We’ll take a closer look in another blogpost. So stay tuned!
Hi and welcome to my blog. Here I’ll write about various of topics surrounding things that is interesting me, especially computers and programming. Well, I hope you will enjoy reading my posts. If you have questions or just want to say what you think just send me an e-mail or leave a comment.
Regards,
Robert.
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
using System;
class Program
{
public static Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Latest comments