C#
C#
NASA WorldWind is worth a check.
“World Wind lets you zoom from satellite altitude into any place on Earth. Leveraging Landsat satellite imagery and Shuttle Radar Topography Mission data, World Wind lets you experience Earth terrain in visually rich 3D, just as if you were really there.”
WorldWind uses Managed DirectX (MDX) to acheive this. But what caught my attention was /cfWorldWind, when I was reading Mike Hall's weblog. Managed DirectX 3D Mobile (MD3DM) can do wonders and this is a proof of it.
One of the screen shots of /cfWorldWind:
An interesting terminology that I came across while going through the C#LS was "boxing" and "unboxing". Though some may be familiar with these terms, I am new to it.
C# has a "unified type system". ie. all types including primitive ones derive from the type "object". A simple example would be:
class UTSDemo
{
static void Main() {
Console.WriteLine(333.ToString());
}
}
The above sinppet calls the method ToString on the integer literal 333 which results in the output "333".
class Boxing
{
static void Main() {
int intVal1 = 123;
object myObj = i; // boxing
int intVal2 = (int) myObj; // unboxing
}
}
From the above example it is clear that an int value can be converted to an object and then back to an int. This is nothing but boxing and unboxing [Mike doesnt know this one ;-)] When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value, and the value is copied into the box. Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location.
So using this you could write a method which has object type as its parameters or as the return type when you want to make the method general irrespective of the type.
I developed the registration website for DWDN where I had to add a small functionality- when somebody registers for an event, an automated mail was to be sent to him/her to the email id specified during registration.
The harder way out was to use sockets and talk to the SMTP server directly and take the pains of handling various conditions. The simpler alternative is System.Web.Mail. The beauty of .NET lies in its feature-rich classes and the organized way in which everythings has been made available to the end-programmer.
The code was as simple as:
using System.Web.Mail;
MailMessage m=new MailMessage();
m.To=txtemail.Text;
m.Subject="Date with .NET : Registration Acknowledgment";
m.From="xyz@gmail.com";
msg="Hello "+ mytitle + txtname.Text +"\n \n Your registration for the event: \n " + lblevent.Text + " is under process.\nYou will be receiving a confirmation mail.\n Please feel free to mail us on xyz@gmail.com if you need any clarifications. \n Regards";
m.Body=msg;
SmtpMail.SmtpServer="localhost";//fill in the name of the SMTP mail server
SmtpMail.Send(m);//send the mail message
It provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component. It can deliver mail through Win2000 SMTP mail service or any arbitrary mail server.
That’s cool enough. The site is hosted on WebMatrixHostitng and their server provides Authenticated-SMTP only. That’s because of their SPAM-control policy.
I had to add this extra bit of code into what I had done earlier.
m.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
m.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "myusername";
m.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "mypass";
Here comes the problem… I was using VS.NET 2002 and the “Fields” collection for the MailMessage class isn’t defined in 2002. :-( I had no other option but to install VS.NET 2003 and get going with it.
It is always better to understand these concepts with an example rather than trying to understand paragraphs of explanation.
Example:
class sharpTest
{
static void Main() {
string s = "Sunil";
string t = string.Copy(s);
Console.WriteLine(s == t);
Console.WriteLine((object)s == (object)t);
}
}
Output:
True
False
Inference:
Two expressions of type object are considered equal if both refer to the same object, or if both are null.
Two expressions of type string are considered equal if the string instances have identical lengths and identical characters in each character position, or if both are null.
Type conversions are similar to C++. Implicit conversions like int to long are done without any loss of data. Explicit conversions are done with a cast expression.
Arrays in C#
As usual, single-dimensional and multi-dimensional arrays are supported.
// Creates a single-dimensional array of 5 elements
int[] arr = new int[5];
What is to be noted is that arrays are also reference types and hence have to be explicitly allocate the memory by specifying the size of the array.
// Some more arrays
int[,,] a3; // 3-dimensional array of int
int[][] j2; // "jagged" array: array of (array of int)
int[][][] j3; // array of (array of (array of int))
Type system unification
class Test
{
static void Main() {
Console.WriteLine(3.ToString());
}
}
All types including value types are derived from the type object. It is possible to call object methods on any value, even values of “primitive” types such as int.
First time I saw the C# code in a magazine, I thought “Oh Damn it. Another new language, new syntax, new keywords…more confusion”. But now I don’t think so. C# is not completely different from other languages speaking of its syntax and approach. Moving further on, I learnt about the data types in C#.
C# supports two kinds of types: value types and reference types. No big deal. The name says it all. In case of values types, the variable stores the value directly and in case of reference types, it stores the reference to the variable (address). Other concepts like possibility of two variables referencing the same object are possible as in the case of C++.
A sample program to understand the types:
using System;
class ClassObj
{
public int num = 0;
}
class ClassTest
{
static void Main()
{
int x = 0;
int y = x;
y = 101;
ClassObj ref1 = new ClassObj();
ClassObj ref2 = ref1;
ref2.num = 999;
Console.WriteLine("Values: {0}, {1}", x, y);
Console.WriteLine("Refs: {0}, {1}", ref1.num, ref2.num);
}
}
C:\WINNT\Microsoft.NET\Framework\v1.0.3705>csc D:\cstypes.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
C:\WINNT\Microsoft.NET\Framework\v1.0.3705>cstypes
Values: 0, 101
Refs: 999, 999
An interesting thing that I’ve observed is the method used to print the formatted output in Console.WriteLine
Console.WriteLine takes a variable number of arguments. The first argument is a string, which may contain numbered placeholders like {0} and {1}. Each placeholder refers to a trailing argument with {0} referring to the second argument, {1} referring to the third argument, and so on. Before the output is sent to the console, each placeholder is replaced with the formatted value of its corresponding argument.
I was going through a list of articles available on the INETA website. I came across an article with the heading “Why C # is sharp and why is VB.NET basic?” Hmmm… However, the author has no intentions of looking down upon VB. I have done quite a few projects in VB 6. I am still a VB enthusiast because what I like most about it, is its simplicity and ability to do amazing stuff with little effort (directly proportional to coding?? Ahem…).
A few days back, I had read an extensive comparison between 11 languages in the September 2004 edition of DIQ (Developer IQ http://www.developeriq.com). It was called “The Great Language Shootout”. The article tabulates the results with C# scoring an overall 68pts and Java landing at 65pts. The article also explains in detail about the evaluation process. These figures are clearly just indicators of a test process and cannot form the basis to decide the power and application of any language or its future.
Unable to resist the temptation, I decided to write a simple HelloWorld… nope not just World… HelloManagedWorld program in C#. Managed because, I’m using the .NET csc compiler.
The code:
using System;
class helloManagedWorld
{
static void Main()
{
Console.WriteLine("Hello Managed World!");
}
}
Some explanation:
The using System; directive references a namespace called System that is provided by the Microsoft .NET Framework class library. This namespace contains the Console class referred to in the Main method. Namespaces provide a hierarchical means of organizing the elements of one or more programs. A “using” directive enables unqualified use of the types that are members of the namespace. The “Hello Managed World!” program uses Console.WriteLine as shorthand for System.Console.WriteLine.
The Main method is a member of the class helloManagedWorld. It has the static modifier, and so it is a method on the class helloManagedWorld rather than on instances of this class.
The entry point for an application—the method that is called to begin execution—is always a static method named Main.
How to compile the geeky way (ie. if you only have the .NET Framework, without VS.NET):
I started off with some errors as usual... case sensitivity and some syntax problems…
C:\WINNT\Microsoft.NET\Framework\v1.0.3705>csc helloWorld.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
helloWorld.cs(4,30): error CS1552: Array type specifier, [], must appear before
parameter name
C:\WINNT\Microsoft.NET\Framework\v1.0.3705>csc helloWorld.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
error CS5001: Program 'helloWorld.exe' does not have an entry point defined
C:\WINNT\Microsoft.NET\Framework\v1.0.3705>csc helloWorld.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.
C:\WINNT\Microsoft.NET\Framework\v1.0.3705>helloWorld
Hello World!
Wasn’t that simple enough? So this is just the beginning. Lets see where I go from here. :-)