100% found this document useful (2 votes)
399 views

Professional C and NET 2021st Edition Nagel 2024 Scribd Download

The document promotes the ebook 'Professional C# and .NET 2021st Edition' by Christian Nagel, available for download on ebookmeta.com. It includes a comprehensive table of contents detailing various chapters covering C# language, libraries, web applications, and more. Additional recommended digital products related to programming and technology are also listed for immediate download.

Uploaded by

mollasymesws
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
399 views

Professional C and NET 2021st Edition Nagel 2024 Scribd Download

The document promotes the ebook 'Professional C# and .NET 2021st Edition' by Christian Nagel, available for download on ebookmeta.com. It includes a comprehensive table of contents detailing various chapters covering C# language, libraries, web applications, and more. Additional recommended digital products related to programming and technology are also listed for immediate download.

Uploaded by

mollasymesws
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get the full ebook with Bonus Features for a Better Reading Experience on ebookmeta.

com

Professional C and NET 2021st Edition Nagel

https://ebookmeta.com/product/professional-c-and-net-2021st-
edition-nagel/

OR CLICK HERE

DOWLOAD NOW

Download more ebook instantly today at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Professional C and NET 2021st Edition Christian Nagel

https://ebookmeta.com/product/professional-c-and-net-2021st-edition-
christian-nagel/

ebookmeta.com

Pro C# 7: With .NET and .NET Core - Eighth Edition Andrew


Troelsen

https://ebookmeta.com/product/pro-c-7-with-net-and-net-core-eighth-
edition-andrew-troelsen/

ebookmeta.com

Professional C++ 6th Edition Gregoire

https://ebookmeta.com/product/professional-c-6th-edition-gregoire/

ebookmeta.com

Clueless Romeo Guys Next Door 3 1st Edition Baylin Crow

https://ebookmeta.com/product/clueless-romeo-guys-next-door-3-1st-
edition-baylin-crow/

ebookmeta.com
Learning Snowflake SQL and Scripting: Generate, Retrieve,
and Automate Snowflake Data 1st Edition Alan Beaulieu

https://ebookmeta.com/product/learning-snowflake-sql-and-scripting-
generate-retrieve-and-automate-snowflake-data-1st-edition-alan-
beaulieu/
ebookmeta.com

Introduction to Modern Modelling Methods (The SAGE


Quantitative Research Kit) 1st Edition D. Betsy Mccoach

https://ebookmeta.com/product/introduction-to-modern-modelling-
methods-the-sage-quantitative-research-kit-1st-edition-d-betsy-
mccoach/
ebookmeta.com

Artificial Intelligence on Medical Data 1st Edition


Mousumi Gupta

https://ebookmeta.com/product/artificial-intelligence-on-medical-
data-1st-edition-mousumi-gupta/

ebookmeta.com

China Trade and Power Why the West s Economic Engagement


Has Failed 1st Edition Stewart Paterson

https://ebookmeta.com/product/china-trade-and-power-why-the-west-s-
economic-engagement-has-failed-1st-edition-stewart-paterson/

ebookmeta.com

Lasl Phermex Data: Volume 1 LASL Phermex Data, Vol. I


Timothy R. Neal (Editor)

https://ebookmeta.com/product/lasl-phermex-data-volume-1-lasl-phermex-
data-vol-i-timothy-r-neal-editor/

ebookmeta.com
Indian constitution law 1st Edition M P Jain

https://ebookmeta.com/product/indian-constitution-law-1st-edition-m-p-
jain/

ebookmeta.com
PROFESSIONAL
C# AND .NET

INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix

▸▸ PART I THE C# LANGUAGE


CHAPTER 1 .NET Applications and Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
CHAPTER 2 Core C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
CHAPTER 3 Classes, Records, Structs, and Tuples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
CHAPTER 4 Object-­Oriented Programming in C#. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
CHAPTER 5 Operators and Casts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
CHAPTER 6 Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
CHAPTER 7 Delegates, Lambdas, and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
CHAPTER 8 Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
CHAPTER 9 Language Integrated Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
CHAPTER 10 Errors and Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
CHAPTER 11 Tasks and Asynchronous Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
CHAPTER 12 Reflection, Metadata, and Source Generators. . . . . . . . . . . . . . . . . . . . . . . 307
CHAPTER 13 Managed and Unmanaged Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

▸▸ PART II LIBRARIES
CHAPTER 14 Libraries, Assemblies, Packages, and NuGet. . . . . . . . . . . . . . . . . . . . . . . . 377
CHAPTER 15 Dependency Injection and Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . 392
CHAPTER 16 Diagnostics and Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
CHAPTER 17 Parallel Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
CHAPTER 18 Files and Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
CHAPTER 19 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
CHAPTER 20 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
CHAPTER 21 Entity Framework Core. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
CHAPTER 22 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
CHAPTER 23 Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668

Continues
▸▸ PART III WEB APPLICATIONS AND SERVICES
CHAPTER 24 ASP.NET Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
CHAPTER 25 Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
CHAPTER 26 Razor Pages and MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752
CHAPTER 27 Blazor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
CHAPTER 28 SignalR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801

▸▸ PART IV APPS
CHAPTER 29 Windows Apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
CHAPTER 30 Patterns with XAML Apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876
CHAPTER 31 Styling Windows Apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
PROFESSIONAL

C# and .NET
PROFESSIONAL

C# and .NET
2021 Edition

Christian Nagel
Copyright © 2022 by John Wiley & Sons, Inc. All rights reserved.

Published by John Wiley & Sons, Inc., Hoboken, New Jersey.

Published simultaneously in Canada.

ISBN: 978-­1-­119-­79720-­3
ISBN: 978-­1-­119-­79722-­7 (ebk)
ISBN: 978-­1-­119-­79721-­0 (ebk)

No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the
1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through
payment of the appropriate per-­copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923,
(978) 750-­8400, fax (978) 750-­4470, or on the web at www.copyright.com. Requests to the Publisher for permission should
be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201)
748-­6011, fax (201) 748-­6008, or online at http://www.wiley.com/go/permission.

Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book,
they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and
specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be
created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be
suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall
be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental,
consequential, or other damages.

For general information on our other products and services or for technical support, please contact our Customer Care
Department within the United States at (800) 762-­2974, outside the United States at (317) 572-­3993 or fax (317) 572-­4002.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic formats. For more information about Wiley products, visit our web site at www.wiley.com.

Library of Congress Control Number: 2021939983

Trademarks: WILEY, the Wiley logo, Wrox, the Wrox logo, and Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries,
and may not be used without written permission. All other trademarks are the property of their respective owners. John
Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

Cover image: © Henrik5000/Getty Images


Cover design: Wiley
This book is dedicated to my family—­Angela,
Stephanie, Matthias, and Katharina—­I love you all!
ABOUT THE AUTHOR

CHRISTIAN NAGEL is a Microsoft MVP for Visual Studio and Development Technologies
and has been Microsoft Regional Director for more than 15 years. Christian is the founder
of CN innovation, where he offers coaching, training, code reviews, and assistance with
architecting and developing solutions using Microsoft technologies. He draws on more than
25 years of software development experience.
Christian started his computing career with PDP 11 and VAX/VMS systems at Digital
Equipment Corporation, covering a variety of languages and platforms. Since 2000, when
.NET was just a technology preview, he has been working with various technologies to
build .NET solutions. Currently, he mainly coaches people on developing and architecting
solutions based on .NET and Microsoft Azure technologies, including Windows apps, ASP.NET Core, and .NET
MAUI. A big part of his job is helping companies move their solutions to Microsoft Azure.
Even after many years in software development, Christian still loves learning and using new technologies and
teaching others how to use them. Using his profound knowledge of Microsoft technologies, he has written numer-
ous books and is certified as a Microsoft Certified Trainer, Azure Developer Associate, DevOps Engineer Expert,
and Certified Solution Developer. Christian speaks at international conferences such as Microsoft Ignite (previ-
ously named TechEd), BASTA!, and TechDays. You can contact Christian via his website at www.cninnovation
.com, read his blog at csharp.christiannagel.com, and follow his tweets at @christiannagel.

ABOUT THE TECHNICAL EDITOR

ROD STEPHENS is a long-­time developer and author who has written more than 250 mag-
azine articles and 35 books that have been translated into languages around the world.
During his career, Rod has worked on an eclectic assortment of applications in such fields
as telephone switching, billing, repair dispatching, tax processing, wastewater treatment,
concert ticket sales, cartography, and training for professional football teams.
Rod’s popular C# Helper website (www.csharphelper.com) receives millions of hits per
year and contains tips, tricks, and example programs for C# programmers. His VB Helper
website (www.vb-­helper.com) contains similar material for Visual Basic programmers.
You can contact Rod at RodStephens@csharphelper.com or RodStephens@vb-­helper.com.
ACKNOWLEDGMENTS

I WANT TO THANK Charlotte Kughen. For many years and many editions of this book, she has made my text
so much more readable. Often, I completed chapters late in the evening, when I miss things as I turn sentences
around. Charlotte was of enormous help in changing my ideas into great readable text. Charlotte, big thanks for
your continued support with these editions; I’m looking forward to working together in the future as well.
Special thanks also go to Rod Stephens, the technical editor of this edition. Rod had great comments on my
source code and induced some changes that helped with the quality of the source code. Rod is also the author
of some great books, for example Essential Algorithms: A Practical Approach to Computer Algorithms Using
Python and C# and WPF 3d: Three-­Dimensional Graphics with WPF and C#. These books can be a great addi-
tion for your C# bookshelf.
My thanks also go to the complete team working on the book. In particular, I want to thank István Novak, tech-
nical editor of several previous editions of this book. Now István had the role as technical proofreader to solve
some final issues. I also want to thank Kim Wimpsett, who fixed some more text issues during the production
phase, and Barath Kumar Rajasekaran, who helped the flow during production.
I would also like to thank all the people working on C# and .NET, especially Mads Torgersen, who has worked
with his team and the community to bring new features to C#; Richard Lander from the .NET Core team, with
whom I had great discussions on the content and the direction of the book; and David Fowler, who enhances
.NET not only with performance improvements but also usability. Thanks go to Scott Hanselman—­who I have
known for many years from our time together as Microsoft RDs—­for his great ideas and continuously working
with the community. Thanks go to Don Box, who influenced me in the times before .NET was available about
love and freedom (COM and XML).
This edition of the book was born during the COVID-­19 crisis, which changed the business landscape faster than
everyone thought would have been possible. I didn’t have less business during this time as I worked online from
my home office, but during my career, I’d never had less travel. This available time was completely spent work-
ing on the book. On the other hand, for previous editions of this book, I remember working many hours while
waiting at the airport. This time, I wrote the entire book in my home office. I want to give a big thanks to my
wife and my children for supporting my writing. You’ve been enormously helpful and understanding while I was
working on the book for many nights, weekends, and without a vacation (not only because of the coronavirus
crisis). Angela, Stephanie, Matthias, and Katharina—­you are my loved ones. This would not have been possible
without you.
CONTENTS

INTRODUCTION xxxix

PART I: THE C# LANGUAGE

CHAPTER 1: .NET APPLICATIONS AND TOOLS 3

From .NET Framework to .NET Core to .NET 3


.NET Terms 4
.NET SDK 4
.NET Runtime 5
Common Language Runtime 6
.NET Compiler Platform 6
.NET Framework 6
.NET Core 6
.NET 7
.NET Standard 7
NuGet Packages 8
Namespaces 9
.NET Support Length 9
Application Types and Technologies 10
Data Access 10
Windows Apps 11
Web Applications 11
Services 12
SignalR 12
Microsoft Azure 12
Software as a Service 13
Infrastructure as a Service 13
Platform as a Service 13
Functions as a Service 13
Developer Tools 14
.NET CLI 14
Visual Studio Code 14
Visual Studio Community 14
Visual Studio Professional 15
Visual Studio Enterprise 15
Discovering Diverse Content Through
Random Scribd Documents
laws for ever. Nor was he deceived in his hopes, for Sparta continued to be
one of the greatest of the Greek states, so Plutarch tells us, as long as she
kept the laws of Lycurgus.

[1] Except where noted otherwise, all the quotations in this chapter are
from Plutarch: Life of Lycurgus.

[2] Herodotus, I.

[3] Xenophon: Constitution of the Lacedaemonians.

CHAPTER VI

THE GROWTH OF ATHENS

I. EARLIEST ATHENS

Athens was the most beautiful city in Greece. It grew up at the foot of the
high rock known as the Acropolis, which in the earliest times was the citadel
that defended the city. The Acropolis had very strong walls, and the main
entrance was guarded by nine gates, which must have made it almost
impossible for an enemy to take, and there was a well within the fortress, so
that there was always water for those who defended it. But history has told
us almost nothing about the mighty lords who built this fortress or about the
life of the people over whom they ruled.

But if history is silent, legend has much to say. The earliest rulers of
Athens were Kings, and of these one of the first was Cecrops. All kinds of
stories gathered round his name, and it was believed that he was not
altogether human, but a being who had grown out of the earth and was half-
man and half-serpent. It was when he was King that the contest took place as
to whether Athena, the grey-eyed Goddess of Wisdom, or Poseidon, Lord of
the Sea, should be the special guardian of the city. The victory was awarded
to Athena, who, taking her spear, thrust it into the ground, whereupon an
olive tree marvellously appeared. Poseidon gave the horse as his gift to
Athens, and legend adds that, striking the rock with his trident, he brought
forth clear salt water, which he also gave to the Athenians. For all time the
olive was associated not only with Athena, but with Attica and Athens her
city, and to the Athenian, the sea became almost like a second home.

The ancient kings claimed descent from the gods. They were not only the
lawgivers, but they acted as judges, as chief priests, and in time of war as
generals. All who were oppressed had the right to appeal to the judgment
seat of the King and his decisions were final. Though the King was the
supreme ruler, there were assemblies of the chief men, always called the
Elders, and of the People, who met whenever the King called them together.
These gatherings are important, not because of any real power they
possessed in early times, for they only met to hear what the King intended to
do and never to discuss, but because it was from these assemblies that the
power of the people to govern themselves developed.

The greatest of the early Kings was Theseus, he who slew the Minotaur
and freed Athens from paying tribute to Minos the Sea-King of Crete. His
greatest claim to be held in the remembrance of his countrymen was that it
was believed to have been Theseus who united all Attica under the
leadership of Athens. Before this time all the towns and villages in Attica
had been independent, but he "gathered together all the inhabitants of Attica
into one town, and made them people of one city ... and gave the name of
Athens to the whole state."[1] Legend tells of him that he was good and
merciful to all who were in need, and a protector of all who were oppressed,
but he offended the gods in some way, and died in exile far from Athens.
Long centuries after, Cimon, an Athenian general, took possession of the
island in which it was said that Theseus had been buried. Cimon
had a great ambition to find out the place where Theseus was buried and
by chance spied an eagle on a rising ground, when on a sudden it came
into his mind, as it were by some divine inspiration, to dig there, and
search for the bones of Theseus. There were found in that place a coffin
of a man of more than ordinary size, and a brazen spear-head, and a
sword lying by it, all of which he took aboard his galley and brought with
him to Athens. Upon which the Athenians, greatly delighted, went out to
meet and receive the relics with splendid processions and with sacrifices,
as if it were Theseus himself returning alive to the city. His tomb became
a sanctuary and refuge for slaves, and of all those of mean condition that
fly from the persecution of men in power, in memory that Theseus while
he lived was an assister and protector of the distressed, and never refused
the petitions of the afflicted that fled to him.[2]

II. THE RULE OF THE FEW: THE OLIGARCHY

It is not known with any certainty how long the rule of the Kings lasted in
Athens, but they seem to have slowly lost their power and at last other
magistrates were appointed to help them rule. The earliest Kings had been
hereditary rulers; when they became less powerful, though they were no
longer the sole rulers of Athens, these hereditary Kings still kept their office
for life. Later they ruled for life but were elected; the next change made was
to elect a new king every ten years, and at last the greatest change of all took
place when the old office of King was done away with, and the power that
had once been in the hands of one man was entrusted to three: the Archon, a
Greek title meaning ruler, who was the chief representative of the State and
who gave his name to the year, the King-Archon, who was the chief priest
and who had authority over all the sacrifices offered by the State, and the
Polemarch, or War-Archon, who was the chief general. Six other archons
were also elected whose duty it was to assist the others and to see that the
laws of the State were obeyed.

Not everyone could be an Archon; only men from noble families could be
elected, and so the power passed into the hands of a few men. The rule of a
few is called an oligarchy, and it was the second step the Athenians took on
their way to be a self-governing community.

At first this rule was good, for by experience the nobles learnt a great deal
about the art of governing; they realised that order was better than disorder
in a state, and they set high standards of devotion to public duty. But the
nobles all belonged to one class of people, they were the best educated and
the more wealthy, and instead of using their advantages of position and
education and wealth as a trust for the good of the whole state (the ideal
developed in later years by the Athenians), they grew to consider these
things their own exclusive property and they became very narrow and
intolerant. They considered themselves in every way superior to the common
people, and began to make laws which benefited themselves alone, ignoring
the rights of others, especially those of the poor.

Now the nobles had acquired their power because of their opposition to
the rule of one man, but when the authority had been placed in their hands,
they proved themselves equally unable to be just towards all, and their rule
became as intolerant as that of the Kings. Then it was that their authority
was questioned in its turn, and the people began to ask each other questions.
What is the difference, they asked, between rich and poor, between the noble
and the plain man, between the freeman and the slave? Who, they asked, are
citizens, and what does it mean to be a citizen? The more the people
questioned, the greater grew the oppression and injustice of the nobles, and
conditions in Athens grew very bad. Many things helped to create this spirit
of discontent: there had been wars, the harvests had been bad and famine had
resulted, and there were very harsh laws which allowed debtors who could
not pay their debts to sell themselves as slaves. Quarrels arose, and more and
more the people questioned as to the justice of all this. They said:

But ye who have store of land, who are sated and overflow,
Restrain your swelling soul, and still it and keep it low;
Let the heart that is great within you be trained a lowlier way;
Ye shall not have all at your will, we will not forever obey.[3]

III. SOLON, THE WISE MAN OF ATHENS AND THE RULE OF


THE MANY[4]

It was at this time of confusion and distress that Solon, one of the Seven
Wise Men of Greece, appeared. By birth he was a noble, but he was a poor
man and in the early part of his life he had been a merchant. There came a
time later when the merchant was not looked upon as the equal of the noble,
for Plutarch, in writing the life of Solon about seven hundred years after his
death, makes an apology for his having been engaged in trade.
In his time, as Hesiod says, "Work was a shame to none," nor was any
distinction made with respect to trade, but merchandise was a noble
calling, which brought home the good things which the barbarous nations
enjoyed, was the occasion of friendship with their kings, and a great
source of experience.

Solon enjoyed the experience of travelling and seeing new things, a delight
that remained with him even to the days of his old age, for when he was old
he would say that he
Each day grew older, and learnt something new.

Just before this time Athens had been at war with Megara, a neighbouring
state, over the possession of Salamis, which had formerly belonged to
Athens, an island so near the Athenian harbour that it was absolutely
necessary that it should belong to Athens. But the war had been long and
unsuccessful, and no victory had been gained by either side. The Athenians
were so "tired with this tedious and difficult war that they made a law that it
should be death for any man, by writing or speaking, to assert that the city
ought to endeavour to recover the island." Solon felt this to be a great
disgrace, and knowing that thousands of Athenians would follow, if only one
man were brave enough to lead, he composed some fiery verses which he
recited in the market place.

I come as a herald, self-sent, from Salamis, beautiful island,


And the message I bring to your ears, I have turned it into a song.

Country and name would I change, rather than all men should say,
Pointing in scorn, "There goes one of the cowardly, lazy Athenians,
Who let Salamis slip through their fingers, when it was
theirs for a blow!"

On then to Salamis, brothers! Let us fight for the


beautiful island,
Flinging afar from us, ever, the weight of unbearable shame.[5]

Only parts of these verses have come down to us, but they so inspired the
Athenians that it was determined to make one more effort to regain Salamis,
and this time they were successful. Salamis was recovered, but conditions in
Athens remained as unhappy as before. Solon was now held in such high
honour that we are told, "the wisest of the Athenians pressed him to succour
the commonwealth." He consented, and was elected Archon in 594 B.C.

The first thing Solon did was to relieve the debtors. He did this by
cancelling all debts and by setting free all who were slaves for debt, and by
forbidding by law any Athenian to pledge himself, his wife or his children as
a security for debt. This brought such relief to the state that the act was
celebrated by a festival called the "Casting off of Burdens."

Solon wanted to bring order into the distracted city he loved, for he held
that order was one of the greatest blessings a state could have, so he set to
work to reform the government of the state, to reduce the power of the
nobles and to give justice to the people. "First, he repealed all Draco's laws,"
(Draco had been an earlier lawgiver in Athens), "because they were too
severe, and the punishments too great; for death was appointed for almost all
offences, so that in after times it was said that Draco's laws were written not
with ink, but blood."

Solon reformed the government of the state in such a way, that even the
poorest citizens had political rights. They could not all be Archons, but
Athens, like Sparta and other Greek states, had her general Assembly of the
people, and they could all vote at this, and they could all take part in electing
the magistrates. Whilst recognizing the rights of the poorer citizens, Solon
believed in preserving a certain part of the power of the nobles, and he
arranged the taxation and public service to the state in such a way that the
greater the wealth of a man and the higher his position, the more the state
demanded of him, both in service and money. Solon himself said of these
laws:

I gave to the mass of the people such rank as befitted their need,
I took not away their honour, and I granted nought to their greed;
While those who were rich in power, who in wealth were glorious
and great,
I bethought me that naught should befall them unworthy their
splendour and state;
So I stood with my shield outstretched, and both were safe
in its sight,
And I would not that either should triumph, when the triumph
was not with the right.[6]

Solon did not please everyone with his laws, and when

some came to him every day, to commend or dispraise them, and to


advise, if possible, to leave out, or put in something, and desired him to
explain, and tell the meaning of such and such a passage, he, knowing
that it was useless, and not to do it would get him ill will, it being so hard
a thing, as he himself says, in great affairs to satisfy all sides, bought a
trading vessel, and having obtained leave for ten years' absence, departed,
hoping that by that time his laws would have become familiar.

He stayed away the ten years and then returned to Athens. He took no further
part in public affairs, but was reverenced by all and honoured until his death.

During his travels, Plutarch tells us that he visited Croesus, the rich King
of Lydia. This visit could never have taken place, for Solon died in Athens
just as Croesus came to the throne. As a matter of fact, Plutarch knew that
quite well, but he says that he must tell so famous a story, even if it were not
true, because it was so characteristic of Solon and so worthy of his wisdom
and greatness of mind, and that it would be foolish to omit it because it did
not agree with certain dates about which in any case everybody differed!
They say that Solon, coming to Croesus at his request, was in the same
condition as an inland man when first he goes to see the sea; for as he
fancies every river he meets with to be the ocean, so Solon, as he passed
through the court, and saw a great many nobles richly dressed, and
proudly attended with a multitude of guards and footboys, thought
everyone had been the king, till he was brought to Croesus, who was
decked with every possible rarity and curiosity, in ornaments of jewels,
purple and gold, that could make a grand and gorgeous spectacle of him.
Now when Solon came before him and seemed not at all surprised, he
commanded them to open all his treasure-houses and carry him to see his
sumptuous furniture and luxuries, though he did not wish it; and when he
returned from viewing all, Croesus asked him if ever he had known a
happier man than he. And when Solon answered that he had known one
Tellus, a fellow-citizen of his own, and told him that this Tellus had been
an honest man, had had good children, a competent estate, and died
bravely in battle for his country, Croesus took him for an ill-bred fellow
and a fool. He asked him, however, again, if besides Tellus, he knew any
other man more happy. And Solon replying, Yes, two men who were
loving brothers and extremely dutiful sons to their mother, and when the
oxen delayed her, harnessed themselves to the waggon and drew her to
Hera's temple, her neighbours all calling her happy, and she herself
rejoicing; then, after sacrificing and feasting, they went to rest, and never
rose again, but died in the midst of their honour a painless and tranquil
death. "What," said Croesus angrily, "and dost thou not reckon us
amongst the happy men at all?" Solon, unwilling either to flatter or
exasperate him more, replied, "The gods, O King, have given the Greeks
all other gifts in moderate degree, and so our wisdom, too, is a cheerful
and homely, not a noble and kingly wisdom, and him only to whom the
divinity has continued happiness unto the end, we call happy."

This story is not only characteristic of Solon, but of the Greek spirit. That
spirit did not like the extreme of extravagance and luxury and display, and it
believed that there was glory that money could not buy. The Greek who had
been rewarded by a wreath of olive leaves had achieved the greatest success
known in Greece. This was once told to a noble who had come with the
Persian King to invade Greece, and when he heard it, he exclaimed to the
King: "What kind of men are these against whom thou hast brought us to
fight, who make their contest not for money but for honour!" That was the
spirit of Greece.
IV. THE TYRANTS

Athens did not attain her political freedom without a struggle. She passed
from the rule of One man, the King, to the rule of the Few, the oligarchy, and
then through the legislation of Solon to the rule of the Many, the people. But
during this period of change, attempts were made from time to time by
powerful leaders to get the rule entirely into their own hands. These leaders
who wanted to seize the power and rule alone were called by the Greeks
Tyrants. There was always the danger that such a ruler, with no authority in
the state to control him, would become harsh, and oppressive, but this was
not always the case. Though the rule of one man alone is never the best kind
of rule, some of the Greek Tyrants made a real contribution to the states they
governed. They were generally well-educated men, who encouraged art and
literature; they were always ambitious men, and they often dreamed of
extending their power beyond the limits of their own state, and though it was
a purely personal and selfish ambition, the efforts at realizing it brought the
Greeks into contact with things which had hitherto lain beyond their horizon,
for in the Age of the Tyrants, no Greek had yet dreamed dreams or seen
visions of empire.

A man was not always successful in his efforts to become a Tyrant. About
forty years before Solon was made Archon, Cylon, a rich Athenian, of good
family and popular as a winner at Olympia, tried to seize the power. He
consulted the Oracle, which told him to make the attempt at the time of the
great festival of Zeus. He took this, as all Greeks would, as meaning the
Olympic Games, so he waited until the time came for them, and then he and
his friends attacked the Acropolis and actually took possession of the citadel.
But it seemed that the Oracle, giving one of those answers of which the
meaning was uncertain, had referred to the festival held in honour of Zeus
near Athens and not to that at Olympia, and Cylon's attempt was
unsuccessful. Some of the conspirators fled, and others took refuge in the
Temple of Athena. Here they were safe, for no one would dare touch anyone
who had placed himself under the protection of the goddess in her sanctuary.
But there was no food or drink in the temple, and as nobody brought them
any, some of them died of hunger, and Cylon was forced to escape secretly.
Then the Archon told the remainder that if they would surrender, their lives
should be spared. They consented, but not quite trusting the Archon, they
fastened a long rope to the Statue of Athena and held it as they descended
the hill, so that they might still be secure under the protection of the goddess.
Half-way down the hill, however, the rope broke, and the Archon, declaring
that this showed that Athena had withdrawn her protection, had the men put
to death. This was looked upon as a great crime by the Athenians, for they
considered it not only treachery, but also sacrilege, and it made the Archon
many enemies. These declared that as a punishment for this act a curse
would rest on him and on all his descendants. His family was descended
from Alcmaeon, and so the curse was spoken of as the curse on the
Alcmaeonids, and the enemies of this family always attributed to it any
calamities that happened to the city.

The most famous Tyrant in Athens was Peisistratus. Whilst Solon was
away on his travels, quarrels broke out again, and when he returned, though
he took no active part in affairs, he tried by privately talking with the leaders
of the various factions to restore peace, but he was unsuccessful. "Now
Peisistratus was extremely smooth and engaging in his language, a great
friend to the poor and moderate in his resentments, so that he was trusted
more than the other leaders."[7] In this way he became very popular, and he
deceived people into thinking that he was only desirous of serving the state,
when in reality he was doing all in his power to further his own ambition and
to become sole ruler of Athens. In order to gain supporters, he appeared one
day in the market place in his chariot, which was sprinkled with blood, and
he himself appeared to be wounded. On being asked what was the matter, he
said his enemies had inflicted these injuries upon him. One of his friends
then declared that the Athenians should not permit such a thing to happen,
and advised that a bodyguard of fifty men should accompany him to protect
him from any further assault. This was done, whereupon with their help,
Peisistratus took possession of the Acropolis. But his power was not great
enough to hold it, and he and his followers were driven out of Athens.

Peisistratus soon returned, however, having thought of a curious plot by


which he might deceive the Athenians into believing it to be the will of the
gods that he should rule. During a festival, accompanied by a large number
of youths, he entered Athens in his chariot, and at his side stood a tall and
beautiful woman, dressed as Athena herself and carrying a shield and spear.
The people shouted that the goddess herself had come from Olympus to
show her favour to Peisistratus, and he was received as Tyrant. But again he
was driven out by his enemies. He stayed away ten years, and then once
more he collected an army and advanced on Athens. Once more he was
successful and entered the city. This time no one opposed him, he became
sole ruler and remained so until his death some ten years later.

Peisistratus showed himself to be a wise ruler; he improved the city and


brought water into it by an aqueduct, and he built new roads. Along these
roads, especially in places near springs and fountains, were placed small
statues of Hermes, and on the pedestals under some of them verses were
engraved, perhaps similar to the following lines, to cheer the traveller on his
way:

I, Hermes, by the grey sea-shore


Set where the three roads meet,
Outside the wind-swept garden,
Give rest to weary feet;
The waters of my fountain
Are clear and cool and sweet.[8]

It was Peisistratus who made the law that men wounded in battle and the
families of those who were killed should be cared for by the state. He built a
new Temple to Athena and made her festival more splendid, and he had the
ancient poems of Homer collected and written down, so that they might be
more carefully preserved. But good ruler as he was, he was still a Tyrant,
and during his rule the people were deprived of their right to govern
themselves, but so long as he lived, no one opposed him.

After his death, his sons Hippias and Hipparchus succeeded him, but they
forgot that, after all, they could only remain Tyrants if the people permitted
it, and they grew insolent, harsh and overbearing. Two young Athenians
formed a plot to assassinate these oppressors at the next festival. The day
came, and Hipparchus was slain, though Hippias escaped. The conspirators
were instantly seized and put to death, and Hippias continued to rule alone.
He became more and more cruel and the Athenians were bowed down under
his oppression. At last the Spartans came to their help. They came, because
for some time whenever they sent to Delphi to ask any advice of the Oracle,
the answer always came, "First set Athens free." With this help, Hippias was
driven out and sent into exile.

Athens was free. The rule of the Tyrants was over, and Athens was once
more able to rule herself, to become that state of which, when it was asked
"what shepherd rules and lords it o'er their people?" the answer could be
given, "Of no man are they called the slaves or subjects."[9]

[1] Plutarch: Life of Theseus.

[2] Ibid.

[3] Poem of Solon, from Aristotle on the Athenian Constitution,


translated by P. G. Kenyon. (By permission of Messrs. G. Bell and Sons).

[4] Except where otherwise noted, the quotations in this section are from
Plutarch: Life of Solon.

[5] Poem of Solon, translated by Leslie White Hopkinson.

[6] Poem of Solon, from Aristotle on the Athenian Constitution,


translated by P. G. Kenyon. (By permission of Messrs. G. Bell and Sons).

[7] Plutarch: Life of Solon.

[8] Written by Anyte, a poetess, probably in the 4th century B.C.,


translated by Sir Rennell Rodd in Love, Worship and Death.

[9] Aeschylus: The Persians.

CHAPTER VII

GREEK COLONIES
I. THE FOUNDING OF A COLONY

The Greeks were a sea-faring people, and they were an adventurous


people. Their own land was small, but the islands of the Aegean formed
stepping-stones, as it were, to the coast of Asia Minor, and the Aegean world
was very familiar to the Greek sailor. Greek galleys were found in most
ports, and the Greek trader became a formidable rival of the Phoenician.

As they sailed from island to island and on to the mainland, the Greeks
came to realize that some of these places would make suitable homes, and by
degrees they began to colonize them; that is to say, parties of settlers went
from their mother-cities to found new homes overseas. Pioneers,
adventurous explorers, had always gone out first and brought back reports of
the new land. A suitable site required a good water supply, and fertile land
where corn could be grown, and the vine and the olive cultivated. The
settlers needed timber from which they could build their ships, and of course
a good harbour was necessary. They also hoped to find friendly natives who
would help them in their farm-work and who would in no way oppose them
or interfere with their plans. The natives must have looked with eyes of
wonder upon the newly arrived Greeks. Most of them had never seen men of
this kind before. The only foreign traders they knew were the Phoenicians,
and they came only to trade, to exploit the people and to exhaust the
resources of the place in order to gain gold. They knew only these "greedy
merchant men with countless gauds in black ships."[1] But these newcomers
were different. A Greek poet has described this Greek adventure over the
sea, and the wonder of those who received the strangers:

A flash of the foam, a flash of the foam,


A wave on the oarblade welling,
And out they passed to the heart of the blue:
A chariot shell that the wild waves drew.
Is it for passion of gold they come,
Or pride to make great their dwelling?[2]
All kinds of considerations took the Greeks over the sea to found new
homes for themselves: some of them were discontented with their
government and wanted to go where they could establish a new one; owing
to the increasing population their home-cities became over crowded which
created difficulties in the supply of food, and many thought a new land
would give them greater and better opportunities; others found that the trade
of the colonies was a source of wealth; and others went just for the love of
adventure. Whenever a body of men decided to sail away and found a
colony, they first consulted the Oracle at Delphi as to whether they would be
successful, and whether Apollo approved of the place they had chosen and
would bless their enterprise. They then chose a leader, whose name was
always held in honour and handed down as the founder of the colony. On
leaving the mother-city, the colonists went in procession to the Town Hall
and there they received fire from the sacred hearth, which they took with
them, and from which they kindled the fire on their own sacred hearth in
their new home.

These colonies were quite independent of the mother-city as far as


government was concerned, but the colonists looked back to the home from
which their race had sprung with great affection; wherever they went they
were still Greeks, they spoke the Greek language and they worshipped the
Greek gods.

Colonies were founded not only in the islands of the Aegean, but along
the coasts of the Black Sea, which the Greeks called the Euxine. These latter
colonies, of which Byzantium (the ancient name for Constantinople) was the
greatest, became very important to the Greeks, for they supplied them with
grain which grew abundantly on the northern shores, and with iron from the
Hittite land in the South-East.

The greatest of all the colonies in the East were the Ionian colonies, those
in the eastern part of the Aegean and on the coast of Asia Minor. The Greeks
who colonized them were descended from the Ionian tribes who had settled
in Greece, and so this whole region became known as Ionia. Herodotus tells
us that the "Ionians had the fortune to build their cities in the most
favourable position for climate and seasons of any men whom we know."
Miletus was the greatest of the Ionian cities, and it developed a very rich
civilization some time before the great days of Athens.
Great thinkers came out of Ionia. Thales, one of the Seven Wise Men of
Greece, the philosopher and man of science, studied the heavens, and he
foretold an eclipse of the sun in a certain year, which came to pass. The
Babylonians before him had made similar studies, but he carried on their
work and made greater advances. He questioned in his mind what his
discoveries might mean, and for the first time in the world he declared that
the movements of the sun and moon and stars were determined by laws, and
that the eclipse of the sun was due to certain movements of the heavenly
bodies, and had nothing to do with the anger of the Sun-God. This was the
first step in the freeing of men's minds from superstition, and though man
had a long way to go and many things to learn before he could take the
second step, it was Thales of Miletus and other Ionian philosophers in the
sixth century B.C. who first set men to thinking about the real meaning of
the things they saw about them in the world of nature. What we to-day call
science was born in Ionia more than two thousand years ago. Many wise
sayings of Thales have been preserved. It was he who said: "God is the most
ancient of all things, for He has no birth: the world is the most beautiful of
all things, for it is the work of God: ... time is the wisest of all things, for it
finds out everything."[3]

Another wise man of science who lived in Miletus was Anaximander. He


was one of the earliest mapmakers, and he and Hecataeus, who wrote a
Geography as a "text to Anaximander's map," were amongst the first
thinkers who developed the science of Geography.
TEMPLE OF POSEIDON AT PAESTUM.
End of 6th Century B.C.
Paestum, the ancient Poseidonia, was a flourishing Greek Colony in
Italy. The colony was founded early in the 6th Century B.C. and in
ancient times it was famous for its roses. The temple is one of the best
preserved Greek temples out of Attica

The Ionian colonies could claim poets as well as men of science. Chios is
said to have been the birthplace of Homer, and Lesbos, one of the largest of
the island colonies, was famous as the home of Sappho, not only the first
woman whose poetry has come down to us, but one of the great poetesses of
the world. Unfortunately we have only a few fragments of her poems.

THE GIFTS OF EVENING


Thou, Hesper, bringest homeward all
That radiant dawn sped far and wide,
The sheep to fold, the goat to stall,
The children to their mother's side.[4]

The face of Greece was turned towards the East, but adventurous spirits
have always turned towards the unknown West; the Phoenicians had already
explored western regions and the Greeks soon followed. The Elysian Fields
lay to the west and what might man not discover if he sailed in that
direction? The Greeks did not find the Elysian Fields, but they did what
proved to be of the most momentous importance in the history of the
civilization of the world. They founded colonies in the south of Italy, and
these became so flourishing that the whole region was known as Magna
Graecia. These Greeks brought their writing, their art, and their poetry and
planted them securely in the land that was one day to be ruled by a city,
which was then only a little settlement at the foot of seven hills. Rome
became mightier than Greece in the art of governing a great empire, and the
day was to come when she would rule Greece herself, but in the
development of her civilization Rome acknowledged the Greeks as her
teachers.

Other Greek colonies were founded at Syracuse in Sicily, and along the
north coast of the Mediterranean to what is now Marseilles, and in the south
a few were established along the shores of Africa to Naucratis in Egypt. The
colonies in the south of Spain and along the north coast of Africa from the
Pillars of Hercules to Carthage were in the hands of the Phoenicians, but by
the end of the sixth century B.C. the prevailing civilization in the
Mediterranean was Greek.

II. IONIA AND LYDIA

The Ionian colonies occupied the coast land of Asia Minor, but the
mainland behind them was the Kingdom of Lydia. For a long time the
Ionians lived in peace, developing their science, thinking out their ideas, and
growing in power. But at the beginning of the sixth century B.C. a new race
of kings came to the Lydian throne. They were vigorous and ambitious, and
did not approve of the important coast towns with good harbours being in
the hands of Greeks. So they attacked them, beginning with Miletus which
was besieged. The siege lasted eleven years, but the city did not surrender.
At last the Lydians realized that Miletus was being saved by her harbour, and
though it could get no food or supplies of any kind by land, everything
needed was brought to the city by water. So the King of Lydia gave up the
idea of conquering Miletus, and he made a treaty of peace with her.

It was probably not only the impossibility of conquering a seaport that


made the King of Lydia give up the siege of Miletus, but the knowledge that
a war cloud had arisen in the east which was steadily drawing nearer his
land. This was the army of the Medes, a nation which had already helped to
destroy Assyria, and whose army was now coming towards Lydia. Several
battles took place with no very decisive result, but at length the two armies
met in a battle
in which it happened, when the fight had begun, that suddenly the day
became night. And this change of the day Thales the Milesian had
foretold to the Ionians. The Lydians, however, and the Medes, when they
saw that it had become night instead of day, ceased from their fighting
and were much more eager both of them that peace should be made
between them.[5]

So peace was made, and soon after the King of Lydia died, and Croesus
succeeded him.

Now the Ionian cities, when they saw their independence threatened,
ought to have combined together and made a joint stand against their
enemies, but each separate city so prized its independence and so feared
anything that might even seem to lessen it, that they stood alone, and when
Croesus, being at peace with the Medes, determined to get possession of
these Ionian cities, he was able to attack them one by one and to overpower
them. He allowed them to keep their own independent government, but he
required them to pay him a regular yearly tribute. This was the first time in
Greek history that Greeks had paid a tribute to anybody; before the reign of
Croesus, all Greeks everywhere had been free. Croesus left a certain amount
of independence to the Ionian cities, because of his admiration for the
Greeks and their civilization. He sent rich and splendid gifts to Apollo, and
in return was made a citizen of Delphi, and at the Pythian Games his envoys
were given special seats of honour.

By this time Cyrus, the Mede, had become King of Persia, and Croesus
watched his increasing power with great anxiety. He saw that war was bound
to come, so he sent a message to the Oracle at Delphi asking if he should
march against the Persians. What Herodotus called a "deceitful" answer
came back, that if he crossed the river Halys a great empire would be
destroyed. Thinking, of course, that this meant the destruction of the Persian
empire, Croesus crossed the river and met Cyrus in battle. Now the Lydians
were famous for their horses, and horsemen were an important part of their
army. Cyrus knew this, so he thought of a plan whereby he might defeat
them. He ordered all the camels which were in the rear of his army carrying
the provisions and baggage, to be unloaded and the camels brought to the
front, and there well-armed men were mounted on them. He did this
"because the horse has a fear of the camel and cannot endure either to see his
form or to scent his smell; and so soon as the horses scented the camels and
saw them, they galloped away to the rear, and the hopes of Croesus were at
once brought to nought."[6]

The Lydians were defeated and withdrew into Sardis, the capital. But
after a short siege Cyrus took the city, and Croesus lost his kingdom. He did
not want to fall into the hands of the Persians, so he had a great pyre erected,
and after pouring out a libation to the gods, he mounted it and bade his
slaves set it on fire that he might perish in the flames, rather than fall alive
into the hands of his conqueror. But suddenly clouds arose in the sky and
rain fell, extinguishing the flames. It was thought that this must be the doing
of Apollo, to whom Croesus had always shown much honour, and hearing of
it, Cyrus commanded that he should be taken down from the pyre and
brought into his presence. "Croesus," he asked him, "what man was it who
persuaded thee to march upon my land and so to become an enemy to me
instead of a friend?" And Croesus answered,
O King, that I did this was to your gain and my loss, and the fault lies
with the god of the Hellenes who led me to march against you with my
army. For no one is so senseless as to choose of his own will war rather
than peace, since in peace the sons bury their fathers, but in war the
fathers bury their sons. It was the will, I suppose, of the gods that these
things should come to pass thus.[7]

Lydia was now added to the Persian Empire and only the Ionian cities
were still independent. But even in the face of the great danger from Persia,
they did not unite, and one by one Cyrus conquered them until Ionia had
been reduced to subjection, and when the cities on the mainland had been
conquered, then the Ionians in the islands, being struck with fear by these
things, gave themselves to Cyrus, who passing over the upper parts of Asia,
subdued every nation, passing over none.[8]

And thus it came about, that the Greeks who lived in Asia lost their
independence, and became subject to the Great King of Persia.

[1] Odyssey, XV.

[2] Euripides: Iphigenia in Tauris, translated by Gilbert Murray.

[3] Diogenes Laertius.

[4] Poem of Sappho, translated by Sir Rennell Rodd in Love, Worship and
Death.

[5] Herodotus, I.

You might also like