Programming Language: Tutorial For Beginners PDF
Programming Language: Tutorial For Beginners PDF
Language
Tutorial for Beginners pdf
www.genial-code.com
Structure of a C# Program
// Specify namespaces we use classes from here
using System;
using System.Threading; // Specify more specific namespaces
namespace AppNamespace
{
// Comments that start with /// used for
// creating online documentation, like javadoc
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
// .. Code for class goes here
}
}
Defining a Class
class Class1
{
static void Main(string[] args)
{
// Your code would go here, e.g.
Console.WriteLine("hi");
}
/* We can define other methods and vars for the class */
// Constructor
Class1()
{
// Code
}
// Some method, use public, private, protected
// Use static as well just like Java
public void foo()
{
// Code
}
// Instance, Static Variables
private int m_number;
public static double m_stuff;
}
C# Basics
If MSDN is installed
Online help resource built into Visual Studio .NET.
Help Menu, look up C# programming language
reference
Dynamic Help
If MSDN is not installed, you can go online to
access the references. It is accessible from:
http://msdn.microsoft.com/library/default.asp
You will have to drill down to VS.NET,
Documentation, VB and C#, and then to the C#
reference.
Both include numerous tutorials, or search on
keywords
Basics: Output with
WriteLine
System.Console.WriteLine() will output a string to the
console. You can use this just like Java’s
System.out.println():
System.Console.WriteLine(“hello world “ + 10/2);
will output:
hello world 5
We can also use {0}, {1}, {2}, … etc. to indicate
arguments in the WriteLine statement to print. For
example:
Console.WriteLine(“hi {0} you are {0} and your age is {1}”,
“Kenrick”, 23);
will output:
hi Kenrick you are Kenrick and your age is 23
WriteLine Options
SavingsAccount a = new
SavingsAccount(0.05);
Console.WriteLine(a.m_amount);
Console.WriteLine(a.m_interest_rate);
Console.WriteLine(a.GetInfo());
The ref keyword must be used in both the parameter declaration of the
method and also when invoked, so it is clear what parameters are
passed by reference and may be changed.
Outputs the value of 1 since variable a in foo is really a reference to
where x is stored in Main.
Passing Reference
Variables
If we pass a reference variable (Objects, strings,
etc. ) to a method, we get the same behavior as
in Java.
Changes to the contents of the object are
reflected in the caller, since there is only one
copy of the actual object in memory and merely
multiple references to that object.
Passing a Reference
Variable
Consider the following:
public static void foo(string s)
{
s = "cow";
}