Getting Started with C Sharp - Pat Turner - E-Book

Getting Started with C Sharp E-Book

Pat Turner

0,0
2,99 €

oder
-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung


This title is one of the "Essentials" IT Books published by TechNet Publications Limited.
This Book is a very helpful practical guide for beginners in the topic , which can be used as a learning material for students pursuing their studies in undergraduate and graduate levels in universities and colleges and those who want to learn the topic via a short and complete resource.
We hope you find this book useful in shaping your future career.

This book will be available soon...

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Pat Turner

Getting Started with C Sharp

BookRix GmbH & Co. KG81371 Munich

Table Of Contents

Table of Contents

The Story behind the Succinctly Series of Books

Chapter 1   Introducing C# and .NET

Chapter 2   Coding Expressions and Statements

Chapter 3   Methods and Properties

Chapter 4   Writing Object-Oriented Code

Chapter 5   Handling Delegates, Events, and Lambdas

Chapter 6   Working with Collections and Generics

Chapter 7   Querying Objects with LINQ

Chapter 8   Making Your Code Asynchronous

Chapter 9   Moving Forward and More Things to Know

Detailed Table of Contents

Chapter 1 Introducing C# and .NET

Chapter 1  Introducing C# and .NET

Welcome to C# Succinctly. True to the Succinctly series concept, this book is very focused on a single topic: the C# programming language. I might briefly mention some technologies that you can write with C# or explain how a feature fits into those technologies, but the whole of this book is about helping you become familiar with C# syntax.

In this chapter, I'll start with some introductory information and then jump straight into a simple C# program.

What can I do with C#?

What can I do with C#?

C# is a general purpose, object-oriented, component-based programming language. As a general purpose language, you have a number of ways to apply C# to accomplish many different tasks. You can build web applications with ASP.NET, desktop applications with Windows Presentation Foundation (WPF), or build mobile applications for Windows Phone. Other applications include code that runs in the cloud via Windows Azure, and iOS, Android, and Windows Phone support with the Xamarin platform. There might be times when you need a different language, like C or C++, to communicate with hardware or real-time systems. However, from a general programming perspective, you can do a lot with C#.

What is .NET?

What is .NET?

.NET is a platform that includes languages, a runtime, and framework libraries, allowing developers to create many types of applications. C# is one of the .NET languages, which also includes Visual Basic, F#, C++, and more.

The runtime is more formally named the Common Language Runtime (CLR). Programming languages that target the CLR compile to an Intermediate Language (IL). The CLR itself is a virtual machine that runs IL and provides many services such as memory management, garbage collection, exception management, security, and more.

The Framework Class Library (FCL) is a set of reusable code that provides both general services and technology-specific platforms. The general services include essential types such as collections, cryptography, networking, and more. In addition to general classes, the FCL includes technology-specific platforms like ASP.NET, WPF, web services, and more. The value the FCL offers is to have common components available for reuse, saving time and money without needing to write that code yourself.

There's a huge ecosystem of open-source and commercial software that relies on and supports .NET. If you visit CodePlex, GitHub, or any other open-source code repository site, you'll see a multitude of projects written in C#. Commercial offerings include tools and services that help you build code, manage systems, and offer applications. Syncfusion is part of this ecosystem, offering reusable components for many of the .NET technologies I have mentioned.

Writing, Running, and Deploying a C# Program

Writing, Running, and Deploying a C# Program

The previous section described plenty of great things you can do with C#, but most of them are so detailed that they require their own book. To stay focused on the C# programming language, the code in this book will be for the console application. A console application runs on the command line, which you'll learn about in this section. You can write your code with any editor, but this book uses Visual Studio.

Note: The code samples in this book can be downloaded at https://bitbucket.org/syncfusiontech/c-succinctly.

Starting a New Program

You'll need an editor or Integrated Development Environment (IDE) to write code. Microsoft offers Visual Studio (VS), which is available via Community Edition as a free download for training and individual purposes (https://www.visualstudio.com/en-us/products/vs-2015-product-editions.aspx). There are other development tools, but you can also use any editor, including Notepad. Notepad++ is another editor that does syntax highlighting, but there are many more available. Essentially, you just need the ability to type a text document. Pick your editor or IDE of choice and it will work for all programs in this book.

Note: You need to use Visual Studio 2015 to compile the samples in this book.

To get started, we need a program to run. In VS, select File > New > Project, then select Installed > Templates > Visual C# in the tree on the left, and finally select the Console Application project type. Name the solution Chapter01, name the project Greetings, set the location to your preference, and click OK. This will create a new solution for you. Delete the Program.cs file and add a Greetings.cs file. In any text editor, just create a file named Greetings.cs. The following is a C# program that prints a greeting to the command line.

using System;

 

class Greetings

{

    static void Main()

    {

        Console.WriteLine("Greetings!");

    }

}

Code Listing 1

The class is a container for code, defining a type, named Greetings. A class has members and this example shows a method member named Main. A method is similar to functions and procedures in other programming languages. For desktop application types, like console or WPF, naming a method Main tells the C# compiler where the program begins executing. Both the Greetings class and Main method have curly braces, referred to as a block, indicating beginning and ending scope.

The void keyword isn't a type; it indicates that a method does not return a value. For Main, you can replace void with int, meaning that the program has a return code. This number can be used by command-line shell tools to evaluate the conditions under which the program ended. It is unique to each program and specified by you. Later, you'll learn more about methods and return values.

The static modifier indicates that there is only ever one instance of a Greetings class that has that Main method—it is the static instance. Main must be static, but other methods can omit static, which makes them instance members. This means that you can have many copies of a class or instance with their own method.

Since a program only needs a single Main method, static makes sense. A program that manages customers might have a Customer class and you would need multiple instances to represent each Customer. You'll see examples of instantiating classes in later chapters of this book.

Inside the Main method is a statement that prints words to the command line. The words, enclosed in double quotes, are a string. That string is passed to the WriteLine method, which writes that string to the command line and causes it to move to the next line. WriteLine is a method that belongs to a class named Console. You see in this example, just like the Greetings class, the Console is a class too. This Console class belongs to the System namespace, which is why the using clause appears at the top of the file, allowing us to use that Console class.

The code begins with a using clause for the System namespace. The FCL is grouped into namespaces to keep code organized and avoid clashes between identically named types. This using clause allows us to use the code in the System namespace, which we're doing with the Console class. Without that, the compiler doesn't know what Console means or how to find it, but now C# knows that we're using the System.Console class.

Summary

Summary

This chapter included a couple broader takeaways regarding how C# fits into the .NET Framework ecosystem and how to create a C# program. Remember that C# is a programming language, but it builds programs that use the FCL to run applications managed by the CLR. What this gives you is the ability to compile programs into assemblies that can be deployed and run on any machine that supports the CLR. The program entry point is the Main method. You can use any editor or an IDE like Visual Studio to write your code. To run a program, press F5 in VS or compile with csc.exe on the command line. To deploy, copy the program to a machine with the CLR installed. In the next chapter, you'll learn more about how to code logic in C# using expressions and statements.

Chapter 2 Coding Expressions and Statements

Chapter 2  Coding Expressions and Statements

In Chapter 1, you saw how to write, compile, and execute a C# program. The example program had a single statement in the Main method. In this chapter, you'll learn how to write more statements and add logic to your program. For efficiency, many of the examples in the rest of the book are snippets, but you can still add these statements inside of a Main method to compile and get a better feel for C# syntax. There will be plenty of complete programs too.