[Mono-docs-list] Learning C# chapter is back from the dead ...
PJ Cabrera
pjcabrera@pobox.com
02 Mar 2003 16:53:00 -0400
--=-vLSOXy5peke1U3ew6754
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
I have been swamped with work for about a month. During this busy time,
I have been tracking the mailing list and the documentation in CVS, but
couldn't commit the time to contribute. Now that things have calmed down
some, I have some more time available.
This Sunday, I made some changes to the Learning C# chapter in The Mono
Handbook. I improved the sections dealing with value types and reference
types, cleaned up some paragraphs here and there, and replaced the name
"MonkeyGuide" with "The Mono Handbook" everywhere in the text.
I noticed the Handbook has tutorial chapters for the different Mono
namespaces now, so I dropped the System.* namespace sections from
Learning C#. I'll contribute to the individual namespaces chapters as I
can.
Finally, I also added anchors in the main index.html, to make it
possible to href specific chapters in the Handbook from tutorial
chapters (like I did in Learning C#).
I have included csharp.html and index.html as attachments to this email.
Could someone please review them and check them in for me?
--
PJ Cabrera
pjcabrera at pobox dot com
--=-vLSOXy5peke1U3ew6754
Content-Disposition: attachment; filename=index.html
Content-Type: text/html; name=index.html; charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<head>
<title>The Mono Handbook</title>
</head>
<link rel="stylesheet" type="text/css" href="style.css" />
<body alink=#000000 link=#000000 vlink=#000000>
<div class="topframe" width="100%"><a href="index.html"><img src="mg.png" border=0></a></div>
<div class="navbar" width="100%">The Mono Handbook</div>
<p><a href="preface.html">Preface</a>
<br><a href="about.html">About this document</a>
<br><a href="contributing.html">Contributing</a>
<br><a href="target.html">Target audiencey</a>
<p><h2 class="part"><a name="PART1">PART 1 - Introduction</a></h2>
<p>
<h4 class="chapter"><a name="CHAP1"><b>CHAPTER 1</b> - Mono</a></h4>
<ul class="menu">
<li>1.1 <a href="mono/dotnet.html">What is .NET?</a>
<li>1.2 <a href="mono/mono.html">What is Mono?</a>
</ul>
<h4 class="chapter"><a name="CHAP2"><b>CHAPTER 2</b> Architecture</a></h4>
<ul class="menu">
<li>2.1 <a href="architecture/architecture.html">.NET Framework Architecture</a>
<li>2.2 <a href="mono/monoimpl.html">Mono Implementation</a>
<li>2.3 Comparing Mono and the .NET Framework
<li>2.4 <a href="mono/monoenabled.html">Mono enabled platforms</a>
</ul>
<p><h2 class="part"><a name="PART2">PART 2 - Installing Mono</a></h2>
<p><h4 class="chapter"><a name="CHAP3"><b>CHAPTER 3</b> Getting Started</a></h4>
<ul class="menu">
<li>3.1 <a href="installation/overview.html">Installation overview & considerations</a>
<li>3.2 <a href="installation/chooseide.html">Choosing an Integrated Development Environment (IDE)</a>
</ul>
<h4 class="chapter"><a name="CHAP4"><b>CHAPTER 4</b> Installation</a></h4>
<ul class="menu">
<li>4.1 <a href="installation/linux.html">Linux </a>
<li>4.2 <a href="installation/windows.html">Windows</a>
<li>4.3 <a href="installation/macosx.html">MacOS X</a>
<li>4.4 <a href="installation/freebsd.html">FreeBSD</a>
</ul>
<h4 class="chapter"><a name="CHAP5"><b>CHAPTER 5</b> Configuring the IDE</a></h4>
<ul class="menu">
<li>5.1 <a href="ide/emacs.html">Emacs</a>
<li>5.2 <a href="ide/eclipse.html">Eclipse</a>
<li>5.3 <a href="ide/sharpdevelop.html">#Develop</a>
</ul>
<p><h2 class="part"><a name="PART3">PART 3 - Using Mono</a></h2>
<h4 class="chapter"><p><a name="CHAP6"><b>CHAPTER 6</b> Runtime Environment</a></h4>
<ul class="menu">
<li>6.1 <a href="tools/runtime-intro.html">Introduction</a>
<li>6.2 <a href="tools/mono-cmdlo.html">Using Mono</a>
<li>6.3 <a href="tools/mint-cmdlo.html">Using Mint</a>
</ul>
<h4 class="chapter"><a name="CHAP7"><b>CHAPTER 7</b> C# & VB Compiler</a></h4>
<ul class="menu">
<li>7.1 <a href="tools/cscomp-intro.html">Introduction</a>
<li>7.2 <a href="tools/cscomp-cmdlo.html">Using the C# compiler</a>
<li>7.3 <a href="tools/mbascomp-cmdlo.html">Using the VB.NET compiler</a>
</ul>
<h4 class="chapter"><a name="CHAP8"><b>CHAPTER 8</b> Assembler & Disassembler</a></h4>
<ul class="menu">
<li>8.1 <a href="tools/assembler-intro.html">Introduction</a>
<li>8.2 <a href="tools/monodis.html">Mono Disassembler</a>
<li>8.3 <a href="tools/assembler.html">Mono Assembler</a>
</ul>
<h4 class="chapter"><a name="CHAP9"><b>CHAPTER 9</b> Other Tools</a></h4>
<ul class="menu">
<li>9.1 <a href="tools/nunit.html">Useful tools: NUnit, NAnt, and NGen</a>
<li>9.2 MonoDoc
<li>9.3 <a href="tools/debugger.html">The Mono debugger</a>
</ul>
<p><h2 class="part"><a name="PART4">PART 4 - Developing Applications with Mono</a></h2>
<h4 class="chapter"><p><a name="CHAP10"><b>CHAPTER 10</b> Mono programming languages</a></h4>
<ul class="menu">
<li>10.1 <a href="languages/csharp.html">C#</a>
<li>10.2 Basic.NET
</ul>
<h4 class="chapter"><a name="CHAP11"><b>CHAPTER 11</b> .NET base class libraries</a></h4>
<ul class="menu">
<li>11.1 <a href="base_classes/namespaces.html">Mono Class Library</a>
<li>11.2 Monos additional Classes: Mono.*, etc.
<li>11.3 System.Windows.Forms Classes
</ul>
<h4 class="chapter"><a name="CHAP12"><b>CHAPTER 12</b> Cross Platform Development</a></h4>
<ul class="menu">
<li>12.1 <a href="cross_platform/introduction.html">Introduction</a>
<li>12.2 <a href="cross_platform/gui.html">Fast Cross-platform GUI</a>
<li>12.3 <a href="cross_platform/comprules.html">10 rules to be cross-platform and .NET compatible</a>
</ul>
<h4 class="chapter"><a name="CHAP13"><b>CHAPTER 13</b> Deploying Applications</a></h4>
<ul class="menu">
</ul>
<p><h2 class="part"><a name="PART5">PART 5 - Using advanced technologies</a></h2>
<h4 class="chapter"><a name="CHAP14"><b>CHAPTER 14</b> Threading</a></h4>
<ul class="menu">
</ul>
<h4 class="chapter"><a name="CHAP15"><b>CHAPTER 15</b> Remoting support in Mono</a></h4>
<ul class="menu">
<li>15.1 <a href="remoting/introduction.html">Introduction</a>
<li>15.2 Remoting tutorial
<li>15.3 Examples
<li>15.4 References
</ul>
<h4 class="chapter"><a name="CHAP15"><b>CHAPTER 16</b> Component Interoperability</a></h4>
<ul class="menu">
<li>16.1 <a href="compinterop/com.html">Windows COM</a>
<li>16.2 <a href="compinterop/bonobo.html">Gnome Bonobo</a>
</ul>
<h4 class="chapter"><a name="CHAP17"><b>CHAPTER 17</b> ADO.NET</a></h4>
<ul class="menu">
<li>17.1 <a href="adonet/introduction.html">Introduction</a>
<li>17.1 <a href="adonet/databases.html">Databases supported by Mono</a>
<li>17.1 <a href="adonet/programming.html">Programming with ADO.NET</a>
<li>17.1 <a href="adonet/tools.html">Tools</a>
<li>17.1 <a href="adonet/examples.html">Code Examples</a>
</ul>
<p><h2 class="part"><a name="PART6">PART 6 - Developing GUI Applications with Mono</a></h2>
<h4 class="chapter"><a name="gnome"><b>CHAPTER 18</b> GNOME.NET - A much needed room to breathe</a></h4>
<ul class="menu">
<li>18.1 <a href="gnome/introduction.html">Introduction</a>
<li>18.2 <a href="gnome/installation.html">Installation</a>
<li>18.3 GNOME.NET Tutorials
<ul class="menu">
<li><a href="gnome/bindings/gtk-sharp/gtk-sharp.html">Gtk#</a>
<li><a href="gnome/bindings/gnome/gnome.html">Gnome#</a>
<li><a href="gnome/bindings/glade/glade.html">Glade#</a>
<li><a href="gnome/bindings/gconf/gconf.html">GConf#</a>
<li>Gda#
<li>Gnome-db#
<li><a href="gnome/bindings/gstreamer/gstreamer.html">GStreamer#</a>
<li><a href="gnome/bindings/rsvg/rsvg.html">Rsvg#</a>
</ul>
<li>18.4 <a href="gnome/same_gnome/same_gnome.html">Same Gnome - a Case</a>
<li>18.5 <a href="gnome/documentation/documentation.html">GNOME.NET Documentation</a>
<li>18.6 <a href="gnome/faq.html">GNOME.NET FAQ</a>
</ul>
<h4 class="chapter"><a name="qt"><b>CHAPTER 19</b> QT#</a></h4>
<ul class="menu">
<li>19.1 <a href="qt/introduction.html">Introduction</a>
<li>19.2 <a href="qt/installation.html">Installation</a>
<li>19.3 C# bindings
<li>19.4 <a href="qt/doc.html">Qt# Documentation</a>
<li>19.5 <a href="qt/faq.html">QT# FAQ</a>
</ul>
<p><h2 class="part"><a name="PART7">PART 7 - Mono Web technology</a></h2>
<h4 class="chapter"><a name="CHAP20"><b>CHAPTER 20</b> Creating ASP.NET WebPages</a></h4>
<ul class="menu">
<li>20.1 <a href="aspnet/introduction.html">Introduction</a>
<li>20.2 <a href="aspnet/comparing.html">Comparing ASP.NET to other WebPage technologies</a>
<li>20.3 <a href="aspnet/installation.html">Installation</a>
<li>20.4 <a href="aspnet/programming.html">Programming with ASP.NET</a>
<li>20.5 <a href="aspnet/mobile.html">Programming for mobile devices</a>
</ul>
<h4 class="chapter"><a name="CHAP21"><b>CHAPTER 21</b> Creating ASP.NET Web Services</a></h4>
<ul class="menu">
<li>21.1 Introduction
<li>21.2 What is a WebService?
<li>21.3 Introduction to SOAP
<li>21.4 The Mono/ASP.NET SOAP implementation
</ul>
<p><h2 class="part"><a name="PART8">PART 8 - Mono in Everything</a></h2>
<p>
<h4 class="chapter"><a name="CHAP22"><b>CHAPTER 22</b> Embedding Mono</a></h4>
<ul class="menu">
<li>22.1 <a href="embeding/embeddc.html">Embedding in C</a>
<li>22.2 <a href="embeding/embeddperl.html">Embedding in Perl</a>
</ul>
<p><h2 class="part"><a name="APPENDIX">APPENDICES</a></h2>
<ul class="menu">
<li> A <a href="appendencies/samples.html">Code Samples used in the document</a>
<li> B <a href="appendencies/sampleapps.html">Sample Applications</a>
<li> C <a href="appendencies/presentations.html">Presentations</a>
<li> D <a href="appendencies/rreading.html">Recommened Reading</a>
<li> E <a href="appendencies/resources.html">Links to other Resources</a>
<li> F <a href="appendencies/faq.html">FAQ</a>
<li> G <a href="appendencies/ecma.html">Ecma Standards</a>
<li> H <a href="appendencies/license.html">License</a>
<li> I <a href="appendencies/credits.html">Credits</a>
</ul>
<p>
<div class="copyright">The Mono Handbook - <a href="http://www.go-mono.org">© Copyright 2002 by Johannes Roith & Martin Willemoes Hansen</a></div>
</body>
</html>
--=-vLSOXy5peke1U3ew6754
Content-Disposition: attachment; filename=csharp.html
Content-Type: text/html; name=csharp.html; charset=UTF-8
Content-Transfer-Encoding: 7bit
<html>
<head>
<title>The Mono Handbook - Learning C#</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head><body>
<div class="topframe" width="100%"><a href="../index.html"><img src="../mg.png" border="0"></a></div>
<div class="navbar" width="100%"><a href="../index.html">The Mono Handbook</a> > <b>Learning C#</b></div>
<h1>Learning C#</h1>
by Johannes Roith (<a href="mailto:johannes at jroith dot de">johannes at jroith dot de</a>),
PJ Cabrera (<a href="mailto:pjcabrera at pobox dot com">pjcabrera at pobox dot com</a>)<br>
<h2>Contents</h2>
<ul>
<li>1 <a href="#tutorial-intro">Using this tutorial</a></li>
<li>2 <a href="#what-is-csharp">What is C#?</a></li>
<ul>
<li>2.1 <a href="#helloworld">A very basic example: Hello, World!</a></li>
<ul>
<li>2.1.1 <a href="#compiling">Compiling our example to CIL with the Mono C# compiler</a></li>
<li>2.1.2 <a href="#running">Running our example with the Mono runtimes</a></li>
</ul>
</ul>
<li>3 <a href="#essentials">C# essentials</a></li>
<ul>
<li>3.1 <a href="#namespaces">Importing namespaces</a></li>
<li>3.2 <a href="#variables">Variables and constants</a></li>
<li>3.3 <a href="#types">Types</a></li>
<ul>
<li>3.3.1 <a href="#essential-types">Value or Essential Types</a></li>
<li>3.3.2 <a href="#obj-references">Object references [ could use some more work ]</a></li>
<li>3.3.3 <a href="#strings">Strings [ could use some more work ]</a></li>
</ul>
<li>3.4 <a href="#operators">Operators [ TO DO ]</a></li>
<li>3.5 <a href="#flow-control">Flow control [ TO DO ]</a></li>
<li>3.6 <a href="#methods">Declaring methods [ TO DO ]</a>
<ul>
<li>3.6.1 <a href="#arguments">Arguments [ TO DO ]</a></li>
<li>3.6.2 <a href="#return-values">Return values [ TO DO ]</a></li>
</ul></li>
<li>3.7 <a href="#scope">Scope and Access modifiers [ could use some more work ]</a></li>
<li>3.8 <a href="#classes-intro">Declaring classes [ could use a lot more work ]</a></li>
</ul>
<li>4 <a href="#csharp-oop">Object-oriented programming in C# [ TO DO ]</a></li>
<ul>
<li>4.1 <a href="#csharp-oop">Inheritance basics [ TO DO ]</a></li>
<li>4.2 <a href="#csharp-oop">Interfaces [ TO DO ]</a></li>
<li>4.3 <a href="#csharp-oop">Advanced use of classes [ TO DO ]</a></li>
<ul>
<li>4.3.1 <a href="#csharp-oop">Multiple classes per source file [ TO DO ]</a></li>
<li>4.3.2 <a href="#csharp-oop">Using multiple source files [ TO DO ]</a></li>
</ul>
<li>4.4+ <a href="#csharp-oop">Other C# OO sections as needed [ TO DO ]</a></li>
</ul>
<li>A <a href="#other-resources">Other C# resources [ TO DO ]</a></li>
<li>B <a href="#credits">Credits</a></li>
</ul>
<h2><a name="tutorial-intro">1 Using this tutorial</a></h2>
This tutorial covers both basic concepts of the C# language, and more advanced
object oriented concepts. It is divided into 4 major sections, to better serve
the variety of readers interested in using Mono and C#.
<p>If you are a beginner programmer and want to learn programming in Mono with
C#, you should read this whole tutorial from beginning to end, and later on
refer to other sections of The Mono Handbook to learn other Mono technologies.
We hope we can help you in your quest to become a C# programmer.
<p>If you know a few languages and have built small object oriented systems,
you could skim sections 2 and 3 of this tutorial to learn what makes C#
different, and move on to sections 4, where we introduce object oriented
concepts in C#.
<p>If you have experience building medium to large object oriented systems in
Java, Python, C++, or another object oriented language, and only want to learn
what is different about C# and Mono, then you can skim through sections 2
and 3 of this tutorial.
<p>Once you are comfortable with C# programming from reading this tutorial, you
can move on to other sections of The Mono Handbook, such as
<a href=../index.html#CHAP11>the introduction to the basic CLI framework</a>
and <a href="../index.html#gnome"> the Gnome.Net guide.</a>
<p>If you already have used C# in a .NET environment, and only want to learn
about programming in Mono with C#, then this tutorial is probably not for you.
You should refer instead to <a href=../index.html#PART2>Part 2 of The Mono
Handbook</a>, and then browse the other sections of the Handbook that are of
interest to you.
<p>There are other resources available to learn C#, which are listed in
appendix A. If after reading this tutorial you still need more guidance, you
are welcome to look at that material.
<p>Without much further ado, let's continue with learning C#.
<h2><a name="what-is-csharp"></a>2 What is C#?</h2>
C# is a language created by Anders Heljsberg of Microsoft, as an answer to the
growing popularity of the Java development platform. It supposedly takes the
best features of the Java programming language, adds in some parts of the C++
language, and makes a new language without the detrimental features of either
progenitor.
<p>Microsoft submitted C# and the Common Language Infrastructure to ECMA
(European Computer Manufacturers Association) in 2000. ECMA ratified C# as a
standard in 2001 as ECMA-334. ISO (International Standards Organization)
ratified the ECMA standard in 2002.
<h2><a name="helloworld">2.1 A very basic example: Hello, World!</a></h2>
The first step to learn a new programming language is traditionally to start
with the simple "Hello, World!" program. This program does nothing more than
printing the two words "Hello, World!" at the console, but it starts to make
clear some C# concepts such as using namespaces, declaring classes, and
declaring methods.
<pre class="code">
// Learning C# - HelloWorld.cs
// Lines that start like this one and the one line above are comments.
// Comments are ignored by the compiler and serve as documentation for the programmer reading the code, usually.
/* Comments that span multiple lines can also be written like this.
Don't forget to end it like this. */
// The line below indicates we want to use classes declared in the System "namespace". We will define namespaces later.
using System;
// The line below names our class. We will explain the basics of classes below
class HelloWorld {
/* The line below names the only method of our class.
* It is a "public static" method called Main with no arguments and no return value.
* Later on we will explain methods, arguments, return values, and access modifiers like "public" and "static".
* Note that Main has an uppercase M, unlike the "main with lowercase m" program starting point in Java, C, and C++.
*/
public static void Main() {
// using the Console class after importing the System namespace
Console.WriteLine("Hello, World!"); // This is how we write to the console in C#
}
}
</pre>
<p>Every program we will write in C# is going to have a structure similar to
this one: we declare we want to use of one or more namespaces, then declare at
least one class, and declare at least a static method named Main, which will
contain the lines of code that start our program.
<p>This basic structure can be modified just a little bit, adding command-line
arguments and return values to Main, for a more powerful and dynamic program.
We will examine those modification options later on, but first let's compile
and run our Hello World example.
<h2><a name="compiling">2.1.1 Compiling our example with the Mono C# compiler</a></h2>
Before we can run our example, we have to compile it to the Common
Intermediate Language, or CIL used by the Mono and .NET runtimes. Once you
have installed Mono, you can type the following at the command-line:
<pre class="code">
$ mcs HelloWorld.cs
</pre>
<p>This command produces a file named HelloWorld.exe in the current directory.
This file contains our program, ready to run.
<p>You can pass command-line parameters to the compiler to change the name of
the output file, embed debugging symbols in the generated file, and more. You
can learn more about using the Mono C# Compiler in
<a href=../tools/cscomp-cmdlo.html>section 7.2 of The Mono Handbook</a>.
<p>The Mono C# compiler is command-line compatible with the Microsoft .NET
Framework C# compiler. If you were entering examples from a Microsoft .NET
tutorial or .NET centric book, generally you could substitute <code>mcs</code>
for any mention of <code>csc</code>, and it should work OK.
<h2><a name="running">2.1.2 Running our example with the Mono runtimes</a></h2>
Now we can run our example. We have two choices: if we are running Mono on an
Intel x86 system we can run our example through a JIT-powered runtime, or if
we are running on another platform, such as Sparc, PowerPC, or IBM S/390, we
can run a bytecode interpreter without a JIT.
<p>To run our example through the JIT runtime:
<pre class="code">
$ mono HelloWorld.exe
</pre>
<p>To run with the non-JIT runtime:<br>
<pre class="code">
$ mint HelloWorld.exe
</pre>
<p>You can learn more about using the Mono Runtimes in
<a href=../index.html#CHAP6>chapter 6 of The Mono Handbook</a>.
<p><span style="font-weight: bold;">Note: Unix systems only</span>
<p>In some Unix systems (including Linux) you can add support to load and run
CLI-based programs without having to type the name of the runtime. Once this
feature is enabled, you can just mark the generated file as executable and
type the name of the generated file, like this:
<pre class="code">
$ chmod u+x HelloWorld.exe
$ ./HelloWorld.exe
</pre>
<p>See <a href=../index.html#CHAP4>chapter 4 of The Mono Handbook</a> for
details on how to enable this feature if you are using a Unix system.
<h2><a name="essentials">3 C# essentials</a></h2>
The first important concept you must learn about C# is this one: the first
method that is executed in a C# program is a static method called Main, with
an uppercase M. This static method Main contains the code that will be
executed by your program when it starts.
<p>You can actually declare more than one class in a C# source file, and each
of these classes can have a static method called Main. This can make things
more complicated when you want to run your program. To avoid confusion for
now, we will restrict our examples to one class per source file unless
absolutely necessary. But don't sweat this for now.
<p>In section 4 of this C# tutorial we will introduce multiple classes per
source file, multiple source files, third-party assemblies, and many more
advanced C# concepts.
<h2><a name="namespaces">3.1 Importing namespaces</a></h2>
All classes in C# are organized in <code>namespaces</code>. Namespaces are
used to group related classes under a common name. When you want to use a
group of classes inside your own classes and programs, you usually import
their namespace.
<p>The two examples below explain the concept of importing namespaces. If
they look familiar, that is because they are virtually identical to the first
example in this tutorial, except here we removed all the extra comments.
<p>When you import a namespace, you don't need to specifiy the namespace of
a class when you use that class:
<pre class="code">
// Learning C# - ImportedNamespace.cs
using System;
class HelloWorld {
public static void Main() {
// using the Console class after importing the System namespace
Console.WriteLine ("Hello, World!");
}
}
</pre>
<p>When you <span style="font-weight: bold;">don't</span> import a namespace,
you need to write the namespace in front of the class when you use it:
<pre class="code">
// Learning C# - NoImportedNamespace.cs
class HelloWorld {
public static void Main() {
// using the Console class without importing the System namespace
System.Console.WriteLine ("Hello, World!");
}
}
</pre>
<p>Importing namespaces with the <code>using</code> directive is actually used
to save typing. It's true! In general, even when using a dozen namespaces in
the same program, the class names in each namespace don't collide (generally,
but sometimes they do). So there really is no need to specify the namespace
every time. We save typing as we program by specifying the namespaces we use
most at the top of the source file.
<p>It can also be used to inform the readers of the souce about which group of
classes are used most by the section of code they are reading, but no one
believes that particular white lie. :-)
<p>You will see more advanced use of namespaces, such as the grouping of your
own classes into their own namespace, in section 4 of this tutorial.<br>
<h2><a name="variables">3.2 Variables and constants</a></h2>
Programs are normally written to work with data. These data can be contained
in either variables or constants.
<h3>Variables</h3>
If the data should be changed at runtime it is saved in variables. Variables
represent memory locations where a program can store values as it runs.
<p>You declare a variable like this:
<pre class="code">
int bufferSize = 1024;
</pre>
<p>The first word in the declaration above is the type. In this example, the
variable is of type <code>int</code>. We will be discussing types in more detail in section
3.3.
<p>The next word in the declaration is the variable name, and this variable
is named bufferSize. The next and last parts of the declaration are the
assignment of the initial value to the variable. This variable is being
assigned the value 1024. As mentioned, this is its initial assigned value,
but it can be changed later in the code.
<h3>Constants</h3>
When the data is contained directly in the source and can't be changed as the
program runs, you have an unnamed constant. All the examples so far have used
unnamed constants in the text being written to the console. In the variable
declaration example above, the number 1024 is also an unnamed constant.
<p>A constant can be declared in a manner similar to a variable and given a
name, especially if it is going to be referred to in more than one place in
the code, or to make the code clearer (i.e. to avoid the use of
<span style="font-style:italic;">"magic numbers"</span> in the code). The name
of the constant is hopefully easier to remember or to understand than the
value alone.
<p>A constant that is declared and given a name is known as a named or
declared constant. In the code, they look a lot like variables, except they
are marked with the <code>const</code> keyword.
<pre class="code">
const float ROUNDED_PI = 3.14159;
</pre>
<p><b>Note:</b> constant names are usually in uppercase, but this is only a
convention dictated by programmer custom. The C# language doesn't dictate that
constants have to be in uppercase.
<h2><a name="types">3.3 Types</a></h2>
As explained in the previous section, variables and constants have a
particular type. Unlike some scripting languages, in C# that type can't change
during the execution of the code. A language that has such behavior is called
a type-safe language.
<p>C# has two kinds of types: value types and reference types. The main
difference between them is that value types store the values of their own
data, while reference types typically store a reference to an object and the
contents of the object is stored elsewhere.
<p>This distinction between value types and reference types will become more
clear with some examples. But before we get to the examples, let us examine
the value types.
<h2><a name="essential-types">3.3.1 Value or Essential Types</a></h2>
Computer languages provide the programmer with a set of types where the most
basic pieces of information can be stored. Although object oriented languages
allow very complex systems to be built, these are built with these more basic
types. These are called the essential or intrinsic types of a language. In the
C# language specification, they are called value types.
<p>C# has the following essential types available. In C#, each essential type
has a corresponding equivalent class of the System namespace.
<p><table class="normalfont" cellpadding="5" border="1">
<tbody><tr valign="top">
<td class="csharp"><b>C# type</b></td><td class="csharp"><b>Common Class Library type</b></td><td class="csharp"><b>Possible values</b></td>
</tr>
<tr valign="top">
<td class="csharp"><code>sbyte</code></td><td class="csharp"><code>System.Sbyte</code></td><td class="csharp"><code>-128 to 127</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>short</code></td><td class="csharp"><code>System.Int16</code></td><td class="csharp"><code>-32768 to 32767</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>int</code></td><td class="csharp"><code>System.Int32</code></td><td class="csharp"><code>-2147483648 to 2147483647</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>long</code></td><td class="csharp"><code>System.Int64</code></td><td class="csharp"><code>-9223372036854775808 to 9223372036854775807</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>byte</code></td><td class="csharp"><code>System.Byte</code></td><td class="csharp"><code>0 to 255</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>ushort</code></td><td class="csharp"><code>System.Uint16</code></td><td class="csharp"><code>0 to 65535</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>uint</code></td><td class="csharp"><code>System.UInt32</code></td><td class="csharp"><code>0 to 4294967295</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>ulong</code></td><td class="csharp"><code>System.Uint64</code></td><td class="csharp"><code>0 to 18446744073709551615</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>float</code></td><td class="csharp"><code>System.Single</code></td><td class="csharp"><code>Approximately ±1.5 x 10<sup>-45 </sup> to ±3.4 x 10<sup>38 </sup> with 7 significant figures</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>double</code></td><td class="csharp"><code>System.Double</code></td><td class="csharp"><code>Approximately ±5.0 x 10<sup>-324 </sup> to ±1.7 x 10<sup>308 </sup> with 15 or 16 significant figures</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>decimal</code></td><td class="csharp"><code>System.Decimal</code></td><td class="csharp"><code>Approximately ±1.0 x 10<sup>-28 </sup> to ±7.9 x 10<sup>28 </sup> with 28 or 29 significant figures</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>char</code></td><td class="csharp"><code>System.Char</code></td><td class="csharp"><code>Any Unicode character (16 bit)</code></td>
</tr>
<tr valign="top">
<td class="csharp"><code>bool</code></td><td class="csharp"><code>System.Boolean</code></td><td class="csharp"><code>true or false</code></td>
</tr>
</tbody></table>
<p>One interesting feature of C# is that types <b>MUST ALWAYS</b> have an
assigned value before they are used. In C# there is no such thing as an
implicit assigned value for essential types like in C, C++, and Java, among
other languages. This lack of an implicit value in C# is known as definite
assignment.
<p>This definite assignment feature calls for some explanation by example:
<pre class="code">
// Learning C# - DefiniteAssignmentError.cs
using System;
class Types {
static void Main() {
int val1 = 10; // This variable is being assigned a value when it is declared
int val2; // This variable is not being assigned a value
Console.WriteLine("The value of val1 is {0}", val1);
Console.WriteLine("The value of val2 is {0}", val2);
}
}
</pre>
<p>When you try to compile this code, the C# compiler will give you this error:
<p><pre class="code">
DefiniteAssignment.cs(14) error CS0165: Use of unassigned local variable `val2'
Compilation failed: 1 error(s), 0 warnings
</pre>
<p>This error means you tried to use val2 without assigning a value to it.
The example below is correct. As you can see, val2 is assigned a value just
before we try to use it in the WriteLine statement.
<pre class="code">
// Learning C# - DefiniteAssignment.cs
using System;
class Types {
static void Main() {
int val1 = 10; // This variable is being assigned a value when it is declared
int val2; // This variable is not being assigned a value until later
Console.WriteLine("The value of val1 is {0}", val1);
val2 = 15;
Console.WriteLine("The value of val2 is {0}", val2);
}
}
</pre>
<h2><a name="obj-references">3.3.2 Object References</a></h2>
Since C# is an object oriented language, most of its power comes from the
declaration of classes and from calling code declared in other classes. To use
a class in your code, you generally declare a variable as being of the type of
that class. This kind of declaration is known as an object reference.
<p>To declare an object reference, you simply use the name of a class as the
type in your variable declaration, like this:
<pre class="code">
System.Int32 tempRef = new System.Int32();
</pre>
<p>This code is very similar to our variable declaration example in section
3.2 of this tutorial. In this example, <code>System.Int32</code> is the type
and <code>tempRef</code> is the variable name. To assign an object to a
reference variable, you allocate an instance of a class with the
<code>new</code> keyword. The <code>new</code> keyword allocates the right
size of memory to store the contents of the specified class, and returns the
location of that chunk of memory. This is what we store in the reference
variable: the location of the allocated object.
<p>As you can see in this example, after the <code>new</code> keyword comes
the name of the class for which to allocate memory. Following the name of the
class are a pair of parenthesis, which is the default constructor signature. A
constructor is a kind of procedure for setting up the contents of an allocated
class. We will be discussing class constructors further in section 4 of this
tutorial.
<h2><a name="strings">3.3.3 Strings</a></h2>
The most common class used in perhaps all programs, is the String. Strings are
an interesting type in C# and other languages. It is really a class, but it is
used so often in code, it ought to be considered an essential type.
<p>In C# and other languages, String constants are defined as any text marked
by a matching pair of double quotes, such as the "Hello, World!" in our first
example. In fact, we have been using String constants in all of our examples
so far, and didn't even need to introduce them. Their use is fairly intuitive,
except for some extreme cases such as escape sequences, discussed below.
<p>To declare a variable of type String, you declare it like any other
variable or constant:
<pre class="code">
// Learning C# - Strings.cs
class Strings {
private const String StringConstant1 = "This is a string constant.";
private const String StringConstant2 = "This is a string constant, referenced by a String variable.";
static void Main() {
String StringVariable = StringConstant2;
Console.WriteLine(StringConstant1);
Console.WriteLine(StringVariable);
}
}
</pre>
<h3>Escape Sequences</h3>
Strings can contain special characters by writing a backslash before them.
These codes preceded by a backslash are called a <code>escape sequence</code>.
Let's look at this example:
<pre class="code">
// Learning C# - EscapeSequence.cs
class EscapeSequence {
static void Main() {
Console.WriteLine("Every\n word\n has\n it\'s\n own\n line\n. BEEP!\a");
}
}
</pre>
<p>The different kinds of escape sequences are:
<p><table class="normalfont" cellpadding="10" border="1">
<tbody><tr><td><code>\'</code></td><td><code>single quotation marks</code></td></tr>
<tr><td><code>\"</code></td><td><code>double quotation marks</code></td></tr>
<tr><td><code>\\</code></td><td><code>backslash</code></td></tr>
<tr><td><code>\0</code></td><td><code>NULL character (terminates a C-type string)</code></td></tr>
<tr><td><code>\a</code></td><td><code>Your computer makes a cool sounding "Beep"</code></td></tr>
<tr><td><code>\b</code></td><td><code>backspace</code></td></tr>
<tr><td><code>\f</code></td><td><code>new page</code></td></tr>
<tr><td><code>\n</code></td><td><code>new line</code></td></tr>
<tr><td><code>\t</code></td><td><code>horizontal tabulator</code></td></tr>
<tr><td><code>\v</code></td><td><code>vertical tabulator</code></td></tr>
</tbody>
</table>
<p>
The newline escape sequence is the most commonly used, followed by the
horizontal tabulator and the "literal" escape sequences for single quote,
double quotes, and the backslash. \0 (NULL character) is often used in C and
C++, but rarely needed in Java and C#. </p>
<h2><a name="operators">3.4 Operators</a></h2>
[ TO DO ]
<h2><a name="flow-control">3.5 Flow control</a></h2>
[ TO DO ]
<h2><a name="methods">3.6 Methods</a></h2>
[ TO DO ]
<h2><a name="arguments">3.6.1 Arguments</a></h2>
[ TO DO ]
<h2><a name="return-values">3.6.2 Return Values</a></h2>
[ TO DO ]
<h2><a name="scope">3.7 Scope and access modifiers</a></h2>
The place in your code where you declare a variable or constant can vary a
lot, depending on your intentions as programmer. You can declare things as
fields in a class, or as temporary storage inside a method. And sometimes you
can access variables and constants outside of the area of the code where they
are declared, and sometimes you can not. While this probably sounds very
confusing, we hope we can help clarify this point in the paragraphs below.
<p>[ Note: Provide some examples using public, private, protected, static, and
the C# specific access modifiers. Use several methods to show the effects of
variable declaration scope. ]
<h2><a name="classes-intro">3.8 Declaring and using classes</a></h2>
We will explain classes in greater depth later in section 4 of this tutorial,
but for now we will introduce the mechanics of declaring a class, with some
simplified object oriented concepts here.
<p>C# is 100% object-oriented. This means all code must be in a framed by a
class.
<p>[ TO DO: continue explaning how to declare a class. Refer to a class as
defining the behavior of an object, how objects and classes are the basis of
object oriented programming, yada yada.]<br>
<h2><a name="csharp-oop"></a>4. Object Oriented Programming in C#</h2>
[ TO DO ]
<h2><a name="other-resources"></a>A. Other C# Resources</h2>
[ TO DO ]
<h2><a name="credits"></a>B. Credits</h2>
Authors: Johannes Roith
(<a href="mailto:johannes at jroith dot de">johannes at jroith dot de</a>),
PJ Cabrera
(<a href="mailto:pjcabrera at pobox dot com">pjcabrera at pobox dot com</a>)
<p>This chapter uses some translated content from the
<i class="emphasis">C# Tutorial</i>,
written in Spanish for <i class="emphasis">Mono Hispano</i>
(<a href="http://mono.es.gnome.org">http://mono.es.gnome.org</a>) by Alejandro
Sanchez Acosta, Alvaro del Castillo San Felix, Eduardo Garcia Cebollero, Cesar
Garcia Tapia, Sergio Gomez Bachiller, Roberto Perez Cubero, and Jaime Anguiano
Olarra.<br>
<p>
<div class="copyright">The Mono Handbook -
<a href="http://www.go-mono.org">© Copyright 2003 by Johannes Roith &
Martin Willemoes Hansen</a></div>
</body></html>
--=-vLSOXy5peke1U3ew6754--