无与伦比的恩惠

Thursday, June 9, 2011

Learning C#

with 0 Comment
I never learn c# before, but I guess I have to deal with it for the next 8 weeks.
Hello c#! I am going to learn you! Please be kind to me!

Simple C# Welcome Program: Welcome.cs
// Name space Declaration
using System;
// Program start class
Class WelcomeCSS {
Static void Main(){
// write to console
Console.WriteLine(“Welcome to C# Station Tutorial”);}
}
Namespace declaration, using System; indicates that you are referencing the System namespace. Namespaces contain groups of code that can be called upon by C# programs.
A class is one of a few different types of elements your program can use to describe objects, such as structs, interfaces, delegates and enums.
Main is often called the “entry point”
Console input/ output (I/O)
Console is a class in the System namespace. WriteLine(...) is a method in the Console class. We use the ".", dot, operator to separate subordinate program elements. Note that we could also write this statement as System.Console.WriteLine(...). This follows the pattern "namespace.class.method" as a fully qualified statement. Had we left out the using System declaration at the top of the program, it would have been mandatory for us to use the fully qualified form System.Console.WriteLine(...). 

Warning: C# is case sensitive

How to obtain command-line input?
You must provide an argument on the command-line for the program to work.
// Namespace Declaration
using System;
// Program start class
class NamedWelcome{
static void Main (string[] args)
{
//write to console
Console.WriteLine(“Hello,{0}”,args[0]);
Console.WriteLine(“Welcome to C#”);}
}
Main method now takes parameter. The parameter name is args. The string[] expression defines the type of parameter args. Anytime you add string[]args to the Main method, c# compiler emits code that parses command-line arguments and loads the command-line arguments into args.
Besides comman-line input, another way to provide into to program is via the Console.

Interacting Input Output via Command-Line performed by ReadLine, Write and WriteLine
//Name space declaration
using System;
class InteractiveWelcome{
public static void Main(){
//write to console/get input
Console.Write(“what is your name?:”);
Console.Write(“Hello,{0}!”,Console.ReadLine());
}
}

Here are Console.Write(...) instead of Console.WriteLine(...). The difference is that the Console.Write(...) statement writes to the console and stops on the same line, but the Console.WriteLine(...) goes to the next line after writing to the console.
The first statement simply writes "What is your name?: " to the console.
The second statement doesn't write anything until its arguments are properly evaluated. The first argument after the formatted string is Console.ReadLine().  This causes the program to wait for user input at the console. After the user types input, their name in this case, they must press the Enter key. The return value from this method replaces the "{0}" parameter of the formatted string and is written to the console. This line could have also been written like this: 
string name = Console.ReadLine(); 
Console.Write("Hello, {0}! ", name);
The last statement writes to the console as described earlier. Upon execution of the command-line with "InteractiveWelcome", the output will be as follows:
>What is your Name?  <type your name here> [Enter Key]
>Hello, <your name here>!  Welcome to the C# Station Tutorial!


0 comments :

Post a Comment

Life is Good

Life is Good

Vietnam Coffee lover 2014

Vietnam Coffee lover 2014

Love angel in Perhentian 2014

Love angel in Perhentian 2014

First Marathon Hwaiting 2014

First Marathon Hwaiting 2014

Penang Art Street 2014

Penang Art Street 2014

Legoland Johor Bahru 2013

Legoland Johor Bahru 2013

Mt.Kinabalu I made it! 2013

Mt.Kinabalu I made it! 2013

KLCC tower 2013

KLCC tower 2013

Romance about wedding 2012

Romance about wedding 2012

Korean Traditional 한복 2010

Korean Traditional 한복 2010

Autumn in my heart 2010

Autumn in my heart 2010