GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 5 votes, 5.00 average.
  #1  
Old 20-Sep-2004, 09:46
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Introduction to .NET


Introduction

In my experience I found that people starting to learn a new programming language find it hard to accomplish this by reading “complicated and sophisticated” books written by the experts that developed or helped maintain the respective language. They find easier to understand explanations given by everyday people (like myself), and after finding out in a few, clumsy words what the respective language is about, they can adventure themselves in the wonderful experience of learning from the people who really know what they are talking about!

NOTE: This is only intended as a brief introduction to the world of .NET. More to come, hopefully…


The .NET Framework


The .NET Framework is a platform for building powerful Web Services and applications.
It consists of 3 main parts:
  • CLR (Common Language Runtime)
  • Hierarchical set of unified class libraries
  • ASP.NET (a new component-oriented version of ASP)

Programming languages

“Managed code” producing languages
C#
Visual Basic .NET
JScript .NET
Hybrid (can produce managed and unmanaged code)
Visual C++.NET

SDK download

Just go to http://www.microsoft.com/net. You will be able to get csc (C-sharp Compiler) for free, but this is only a command-line application – you need to buy VS.NET for the working environment (you could do without it).



Some basic concepts and terminology

CLR - Common Language Runtime
It is, basically, the execution engine for .NET Framework applications.

CTSCommon Type System
It is, obviously, the .NET Type System. Common means that it supports the types and operations found in most programming languages

CLSCommon Language Specification
Being a subset of the CTS, it also is what makes .NET so beautiful! It consists a truly guide for programmers developing libraries and compilers. If they stick to this model, the result is that their designed
- libraries: are guaranteed to be perfectly usable from languages supporting the CLS
- compilers: allow the respective languages to integrate with each other

MSILMicrosoft Intermediate Language

MSIL is the instruction-set into which .NET programs are compiled. It is CPU-independent, just like Java .class files (I will get back on this “striking resemblance”  in concept with Sun’s Java)
MSIL, together with the CTS, allows cross-language integration (this really is a new MS-made feature).
In order to execute, the MSIL is first converted to machine code. As opposed to Java, there is NO run-time interpretation involved => a plus in execution speed.

Managed code

I bet you heard this a thousand times. In case you wondered what it meant and didn’t bother to find out just then, here is my dumb description:
Managed code is code written for the CLR, providing special metadata to the runtime. The only .NET programming language that doesn’t produce managed code by default is C++. However, it to can be “persuaded” to do this by using the /CLR switch on the command-line compiler.

Managed data is the data allocated/de-allocated by the runtime’s Garbage Collector (more about gc in a bit). VC++.NET can have access to the benefits of managed data through ME (Managed Extensions).
For instance, in order to have a C++ class derive from a C# one, you need to make it a managed class, as the .NET Framework requires. You can do that by applying the _gc modifier to the class. This signals the compiler the class falls under the rules of Garbage Collection, and every instance of the class will be managed by the gc .
Of course, as a fall back, you won’t be able to use multiple inheritance with your C++ class, as this is forbidden by the .NET Framework. (you can, however, use interface implementation – unfortunately, this doesn’t make the object of my very brief and basic introduction)


The Garbage Collector


Finally!
This concept is familiar to Java programmers, but new to C++ developers. The garbage collector is responsible for de-allocating memory used by objects when they are no longer accessible (they go out of scope).
The developer who wrote the class the object belongs to can write a special clean-up routine called a finalizer. This routine will always execute, no matter when the memory is freed, and should contain code written by the programmer to de-allocate special system resources, like file handles, for instance.
The Garbage Collector will release the other resources when compacting the work set.


C#


What is C#?

Tough question!

Pathetic definition: C# is an Object Oriented programming language, designed by Microsoft, with a clear affiliation to the C/C++ family of languages, but with a higher, more evolved conceptual design. In other words, it is kind of “Microsoft’s Java”. Actually, in their previous version of Visual Studio, they had a Java editor called Visual J++, which they stopped developing once C# came in (there I, of course, J# - very strange to me, as it doesn’t have recognition from Sun and remains only a MS “private” developing environment). Therefore, the nickname is well deserved.

Although Microsoft has tried to illustrate C# as a combination of C++ and Visual Basic, I really don’t see very much of VB in it. Of course, I’m no VB programmer, so I might be wrong…

C# uses the .NET class library (as do all .NET languages). Unlike C++, it is built to work within the .NET Framework. I think ME (Managed Extensions) for Visual C++ were added as a feature and VC was kept in the .NET version of VS.NET because they needed an environment capable of porting *old style* applications to the .NET Framework. I feel that C# (or VB) should be used for everything else.


A very simple C# application

You can build your very first C# application with a simple text editor – let’s say, Notepad! This will only be a console application, but hey, it’s a start…

C-SHARP / C# Code:

class CSharpApplication
{
	public static void Main(string[] args) 
	{
		System.Console.Write("Hello, forum reader!");
		System.Console.WriteLine("\n{0} arguments received", args.Length);

	}
}

Observe how I “cleverly” avoided using the boring “Hello world” example ?
You will run this example from the comand prompt like this:

Code:
>csc applicationName.cs

This if you have the Environment Variables set….(if you don't, than do it, or just go to the right directory; I won't show here how to do it; I just won't!  )


Superficial code walk-through


You’ll notice that, just like in Java, the Main method needs to be encapsulated inside a class (also, you can’t use main).

Instead of calling System.Console.WriteLine, we could have done this:

C-SHARP / C# Code:
// C# Actually 
using System;

class CSharpApplication
{
	public static void Main(string[] args) 
	{
		Console.Write("Hello, forum reader!");
		Console.WriteLine("\n{0} arguments received", args.Length);
 
	}
}

Therefore, System is a namespace we can use to shorten our code! (hilarious explanation, right?)

So, what about this WriteLine? Pascal writeln? C printf? Who knows – this is just what we are stuck with for console output…
We can use as many parameters as we want, by simply incrementing the value inside the brackets ({0},{1},{2} etc.)
Also notice we don’t need to use both args and argv in order to accept parameters, as we would in C++, just a string will suffice.

Some new C# concepts


Introducing Properties

C++ programmers are familiar with this technique, they just don't know what it is! Actually, there are some changes in approach, but the concept is the same.
When declaring private member data to your classes, you should supply methods to acces this data, like so:

C-SHARP / C# Code:
//StarCraft Sample :D
class Zealot
{
	private:
		int shieldEnergy;

	public:
		void SetShieldEnergy(int);
		int GetShieldEnergy();
};

In C#, there is a built-in mechanism implementing this technique, in order to make our work easier and to produce more transparent code:

C-SHARP / C# Code:
//C#, actually
class Zealot
{
	private int shieldEnergy = 0;
	private string protosName = "";

	public int ShieldEnergy
	{
		get 
		{
		   return shieldEnergy; 
		}
		set 
		{
		   shieldEnergy = value; 
		}
	}

	public string Name
    {
        get 
        {
           return protosName; 
        }
        set 
        {
           protosName = value; 
        }
    }
}

I think the similarity is pretty obvious. The get and set are called getter and setter methods.

Notice the value in the setter method? This is also part of the built-in mechanism. It will take the value passed to the setter.
Also, notice that we don't have to fill in ; after the definition of the class. However, we can do that, without the compiler bothering us. This feature is to keep both Java and C++ programmers comfortable.
In C#, just like in Java, you need to supply the modifiers for each field. I think this is intended to keep us from making mistakes (yeah, right, like this little thing could stop us ).
Usually, the property name will be the name of the field, only with a Capitalized first letter. Please stick to this naming scheme, or you will miss out on the whole properties purpose.

Finally, what are they good at?
Well, here is how you'd use them:

C-SHARP / C# Code:
class Zealot
{
	private int shieldEnergy = 0;
	private string protosName = "";

	public int ShieldEnergy
	{
		get 
		{
		   return shieldEnergy; 
		}
		set 
		{
		   shieldEnergy = value; 
		}
	}

	public string ProtosName
    {
        get 
        {
           return protosName; 
        }
        set 
        {
           protosName = value; 
        }
    }

	public static void Main()
    {
        Zealot zealot = new Zealot();


        zealot.ProtosName = "Daak'sar";
        zealot.ShieldEnergy = 150;
        Console.WriteLine("{0} has {1} shield energy left", zealot.ProtosName, zealot.ShieldEnergy);

        
    }

}

So, could you tell (just by thinking in C) that ProtosName and ShieldEnergy are not public member data? Properties will make it easier for programmers working on the same project to continue your work, as by the completely new naming scheme it is easier to find accessors for your data and use it. Of course, this means it is easier for you to get laid off, since anybody can understand your code, and you aren't the only "genius" able to continue your work Just for fun

More about Properties here

Attributes

If you'll read some C# samples, you are bound to come across something like this:

C-SHARP / C# Code:
[STAThread]
int main(void)
{
   // code here
}


So. What is that [STAThread]? That, my forum-reading friends, is a C# attribute. I think that attribute defining can be pretty tricky sometimes, since it involves rather advanced concepts, likeReflection, so I won't go into all that. I just want to mention this is a really important C# concept, and I would strongly suggest you do some serious reading on this. You could start here.
By the way, the [STAThread] attribute is used to define the COM threading model (Single Threaded Apartments). It is pretty obvious the other option would be [MTAThread].
I'll just show you a sample of a .NET environment defined Attribute, Obsolete (as it is pretty easy to use):
C-SHARP / C# Code:
public class CSharpClass
{
   [Obsolete("This method will be removed in future versions", false)]
// if we provide "true" as the second parameter, the compiler will generate an error
// when this method is use; with "false", the programmer will only get a warning
   static void DeprecatedMethod() 
   {
	   System.Console.WriteLine("Oups!"); 
   }

   static void NewMethod() 
   { 
	   System.Console.WriteLine("Finally..."); 
   }
   
   public static void Main( ) 
   {
      DeprecatedMethod();
	  NewMethod();
   }
}

Indexers

This feature enables programmers to treat class instances(objects) as they would arrays. Actually, indexers are sometimes called "virtual arrays", since they enable classes to emulate their behavior. Indexers are closely related to properties. Thus, in order to create an indexer for a class, you create a property with the name this (all C++ programmers can easily figure out why, so I won't insist on this), but also supply it with a set of [], and an "array index" between them.
Here's a short sample:

C-SHARP / C# Code:
class Zealot
{
	private int lifeEnergy = 100;
	private string[] weapons;

    public Zealot()
	{
		this.weapons = new string[7]; 
	}
 
    public string this[int i]
    {
		get
		{
			if ( i >= 0 && i < 7 )
			{
				return ( lifeEnergy > 0 ) ? this.weapons[i] : "Killed in action";	
			}
			else
				return "Out of bounds";
			return "Nothing";	
 		}
		set
		{
			if ( lifeEnergy > 0 && i >= 0 && i < 7 ) 
				this.weapons[i] = value;
		}
	}
}

You will use your brand new indexer like this:

C-SHARP / C# Code:
Zealot zealot = new Zealot();
zealot[3] = "tomahawk";
Console.WriteLine("Q: What's my attack option? \n A: " + zealot[3]);

Got the picture? Hope so… You can read much more here



This is it for now. This should help people completely new to the .NET architecture get an idea about what they are dealing with. If you read this small, clumsy introduction, please don't stop here! There are so many things to learn in this wonderful, brand new world! (personal opinion)

NOTE: I didn’t cover important concepts like Assemblies and Modules here, because I didn’t feel it was the right place (since my introduction was at a very basic level). I promise to get back on this (and lots more) in the upcoming future.
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
Last edited by LuciWiz : 16-Apr-2005 at 06:37.
  #2  
Old 21-Sep-2004, 05:37
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by LuciWiz
You will run this example from the comand prompt like this:

Code:
>csc applicationName.cs

This is, of course, a stupid thing to say. I meant build.
Just like in Java, there are 2 stages in running a program.
1.
Code:
//Java >javac Application.java

=> Application.class

2.
Code:
>java Application
or
Code:
java Application.class

The difference with C# is that it generates an .exe.

1.
Code:
>csc applicationName.cs
=> applicationName.exe
2.
Code:
>applicationName

Now this is running! (terrible sorry about the missusage of words)

Also, I've noticed that my code formatting went crazy when pasting from Word - I would change it, but it seems that the same rules apply to this forum as to the others, and I can't anymore.
Why didn't I do it right away?
Good question...that's me, I guess

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 03-Oct-2005, 14:40
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 225
Kacyndra will become famous soon enough

Re: Introduction to .NET


great tutorial : )
you answered most of my questions with it.
__________________
Xrum!
  #4  
Old 12-Oct-2005, 08:50
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 225
Kacyndra will become famous soon enough

Re: Introduction to .NET


Classes and Structs


Classes and Structs look exactly the same in C#, when they are declared and used. The difference is that one is using stack and the other heap memory.
  • Heap is managed memory, stack is used each time a method is called.
  • All objects must be allocated with a new operator. A new operator implies a heap allocation.
  • Allocations on the stack must be fixed sized. Thus they can be a value type (as we shall see later) or a reference for a reference type. Reference types are not fixed size, and cannot be allocated on the stack.
  • Value types can be allocated on the program stack.
  • Value types can be allocated with a new statment.



classes can be extended (allow for inheritence), structs cannot
structs are fixed size, and so can be allocated on the stack, and allocated in arrays directly.


C-SHARP / C# Code:
using System;

namespace StructVsClass
{
	public class PersonClass 
	{
		public  string name;
		public  int age;


		public override string ToString() 
		{
			return "name = " + name + " age = " + age;
		}
	}

	public struct PersonStruct 
	{
		public  string name;
		public  int age;


		public override string ToString() 
		{
			return "name = " + name + " age = " + age;
		}
	}
	
//notice that the PersonStruct and PersonClass look identical.
//if you were to code, and another programer was put in charge 
//of your code, he'd never know.


	class Class1 
	{
		static void Main(String[] args)
		{
			
			PersonStruct p1;  //do not need to specify NEW 
                                                               //for Struct
			p1.age = 26; 
			p1.name = "Chuck";

			PersonClass p2;      //needs a NEW
//			p2.age = 26;	// Note: Compiler Error here
//			p2.name = "chuck";

			// Allocates 10 PersonStruct objects
			PersonStruct[] ps1 = new PersonStruct[10];

			// Allocate 10 PersonCall references, but no 
                                       //PersonClass Objects.
			PersonClass[] pc1 = new PersonClass[10];

			// Boxing and Unboxing
			PersonClass pc2 = new PersonClass();
			pc2.age = 32;
			pc2.name = "chuck";
			object o = pc2;
			PersonClass pc3 = (PersonClass)o;
			pc3.age = 33;
			// Note pc2 and pc3 are the same object, so
                                      // changes to one
			// effect changes to the other.
			Console.WriteLine("pc2.age = " + pc2.age +
				              " and pc3.age = " + pc3.age);

//output for Class is : 33 33
//basically pc2.age point to a value, when a new
//object O is made, it points to the same value.  Then pc3.age is made, it 
//points to O, which points to pc2. So we now have all 3 objects pointing 
//to the same value. If it was to change, all three objects will also change.

			PersonStruct ps2 = new PersonStruct();
			ps2.age = 32;
			ps2.name = "chuck";
			o = ps2;
			PersonStruct ps3 = (PersonStruct)o;
			ps3.age = 33;
			// Note ps2 and ps3 are different objects, so changes to one
			// do not effect changes to the other.
			Console.WriteLine("ps2.age = " + ps2.age +
				" and ps3.age = " + ps3.age);
			Console.WriteLine("");
//here the output is ps2 = 32 and ps3 = 33.
//when a new object is made in a STRUCT, a copy is made, therefore the old
//value stays the same, when the new one changes.



			// Call by value type and call by reference type

			pc2.age = 33;
			method1(pc2);
			Console.WriteLine("pc2 after call =" + pc2.age);

			ps2.age = 33;
			method2(ps2);
			Console.WriteLine("ps2 after call =" + ps2.age);

			Console.ReadLine();
		}

		public static void method1(PersonClass pc) 
		{
			pc.age = 35;
		}

		public static void method2(PersonStruct ps) 
		{
			ps.age = 35;
		}

	}
}


__________________
Xrum!
  #5  
Old 09-Jun-2007, 06:00
EvoTech EvoTech is offline
New Member
 
Join Date: Jun 2007
Location: Romania
Posts: 15
EvoTech is on a distinguished road

Re: Introduction to .NET


I like the tutorial, just a suggestion, you should split the .NEt in more categories (VB,C#,ASP) so ppl cand find what they are looking exactly.
  #6  
Old 09-Aug-2007, 04:53
juvenile386 juvenile386 is offline
New Member
 
Join Date: Jun 2007
Posts: 28
juvenile386 is an unknown quantity at this point

Re: Introduction to .NET


I was so stupid posting a thread asking what a .net is...
 
 

Recent GIDBlogA Week in Kuwait by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VC++ .NET Windows and interrupts / watchdogs question. bmwrob MS Visual C++ / MFC Forum 0 30-Jun-2004 21:06
UDP Sockets between .NET and VS6 MikeZ MS Visual C++ / MFC Forum 2 19-Feb-2004 10:55
.NET Mobile Controls Device Update 3.0 Available at DiscountASP.NET dasp Web Hosting Advertisements & Offers 0 15-Sep-2003 16:26
IP*Works! CC ICharge .NET Edition Launched at DiscountASP.NET dasp Web Hosting Advertisements & Offers 0 25-Aug-2003 19:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:34.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.