c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I832 materiais10.248 seguidores
Pré-visualização50 páginas
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
151 
Enum
18 Enum
The value type enum assigns names to constants of one of the types byte, short, int and long. As default 
are assigned constants values from 0 onwards, but you can also explicitly assign a constant a value. An 
enumeration is declared in the following manner:
public enum Ugedag : byte
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
Error = 10
}
which declared 8 constants. The type is here byte and the constant Monday has the value 0, Tuesday has 
the value 1, etc. The constant Error is initialized explicitly with the value 10.
In the following method, the user must enter a text: Mo, Tu, We, Th, Fr, Sa or Su. This text is then 
converted to an enum that is printed on the screen.
\u201cThe perfect start 
of a successful, 
international career.\u201d
CLICK HERE 
to discover why both socially 
and academically the University 
of Groningen is one of the best 
places for a student to be www.rug.nl/feb/education
Excellent Economics and Business programmes at:
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
152 
Enum
static void Test1()
{
Ugedag dag;
Console.Write("(Mo, Tu, We, Th, Fr, Sa, Su)? ");
string text = Console.ReadLine();
switch (text)
{
case "Mo":
dag = Ugedag.Monday; break;
se "Tu":
dag = Ugedag.Tuesday; break;
case "We":
dag = Ugedag.Wednesday; break;
case "Th":
dag = Ugedag.Thursday; break;
case "Fr":
dag = Ugedag.Friday; break;
case "Sa":
dag = Ugedag.Saturday; break;
case "Su":
dag = Ugedag.Sunday; break;
default:
dag = Ugedag.Error; break;
}
Console.WriteLine(dag);
}
Note the switch statement that switches on a string. It is allowed in C#. Note also that the program writes 
Sunday, that is the name of the constant.
An enum as above define 8 values , but that does not mean that a variable of that type takes up 8 bytes. 
It takes up only one byte, and it is only a question that there is assigned a name determined by the 
variable\u2019s value.
The default type is int, and you can simply write:
public enum Color
{
Diamonds, Hearts, Spades, Clubs
}
Enum types are effective and it is a better solution than to define a number of constants for a concept, 
and enum types can be used to increase readability.
The above examples concerning enums are found in Exam36.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
153 
Struct
19 Struct
The last value type is a struct that is a structure that resembles a class to the confusion. The main difference 
is that a struct is a value type. Below is a struct which represents a point in a coordinate system:
struct Point
{
public double x;
public double y;
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double Length
{
get { return Math.Sqrt(x * x + y * y); }
}
public override string ToString()
{
return string.Format("({0},{1})", x, y);
}
}
The type is called Point, and there are two instance variables of the type double. This means that every time 
you create a Point there is allocated 16 bytes on the stack. The type have a constructor, which initializes 
the two coordinates, and there is a single property, which returns the distance from the (0,0) to the point. 
In addition there is a ToString() method. Actually, there is also an implicit default constructor that sets 
both variables to 0 \u2013 this constructor can\u2019t be overridden. Below is a method that uses the type Point:
static void Test1()
{
Point p1;
Point p2 = new Point(4, 5);
p1.x = 2;
p1.y = 3;
Point p3 = p1;
p3.y = 8;
Show(p1);
Show(p2);
Show(p3);
Console.WriteLine(p3.Length);
}
private static void Show(Point p)
{
Console.WriteLine("({0},{1})", p.x, p.y);
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
154 
Struct
If you run the program you get:
Explanation
First the method create a Point
Point p1;
That means creating a variable p1 on the stack and the default constructor is executed. Note that it is 
not a reference, but a variable on the stack takes up 16 bytes. Then the method create another variable 
p2, but this time with the new operator:
Point p2 = new Point(4, 5);
and even if p2 is created with new, it is still created on the stack. The new operator is used here to get 
the constructor executed and therefore has a different meaning than it has with a class. As the next point 
values are assigned to the coordinates of p1. Note that it is possible, since both variables are public. Then 
the program create a third variable:
Point p3 = p1;
© Agilent Technologies, Inc. 2012 u.s. 1-800-829-4444 canada: 1-877-894-4414
Teach with the Best. 
Learn with the Best.
Agilent offers a wide variety of 
affordable, industry-leading 
electronic test equipment as well 
as knowledge-rich, on-line resources 
\u2014for professors and students.
We have 100\u2019s of comprehensive 
web-based teaching tools, 
lab experiments, application 
notes, brochures, DVDs/ 
CDs, posters, and more. See what Agilent can do for you.www.agilent.com/\ufb01nd/EDUstudents
www.agilent.com/\ufb01nd/EDUeducators
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
155 
Struct
This means that the program create another variable on the stack, which is a copy of p1. Note that p3 
is a new variable, which has the same value as p1, but this is not a reference to p1. It can be seen by 
changing the value of p3, and that this change does not affect p1.
Comment
The syntax for a struct which is as for a class other than that the word class is replaced by the word struct. 
A struct can have constructors just as a class, properties and methods, and the only difference here is 
that a struct always have a default constructor, which can\u2019t be overwritten. A struct can also implement 
an interface, but a struct can\u2019t inherit. Aside from that there is no difference.
The difference between a struct and a class is thus on the application. Typically, a struct is used to 
encapsulate a few simple data types to achieve a better performance equivalent to that it is a value type. 
Therefore you will often skip properties, making variables public, as is the case in the type Point. When an 
object is allocated on the stack, there can be only one reference to it, and the need for data encapsulation 
is not the same as for heap-allocated objects.
Copying struct\u2019s
When a struct type is a value type, it means that if you assign a variable to another variable, it is a really 
copy
Point p1 = new Point(2, 3);
Point p2 = p1;
where p1 is copied to p2. You must be special aware of that if a struct has instance variables of reference 
type, since it is only the references there are copied and not the objects which they refer. If you are not 
aware of that, it can sometimes produce unexpected results. As an example, consider the class Dice:
class Dice
{
private static Random rand = new Random();
private int eyes;
public Dice()
{
Throw();
}
public int Eyes
{
get { return eyes; }
}
public void Throw()
{
eyes = rand.Next(1, 7);
}
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
156 
Struct
There is nothing to note about it, besides it is a reference type. The following type defines a pair of die \u2013 
a cup with two Dice objects:
struct Par
{
public Dice d1;
public Dice d2;
public void Throw()
{
d1.Throw();
d2.Throw();
}
public override string ToString()
{
return d1.Eyes + " " + d2.Eyes;
}
}
It is a value type with two instance