22
C# Series, Week 0 Jamshid Hashimi, CodeWeekend

Introduction to C# - Week 0

Embed Size (px)

Citation preview

C# Series, Week 0Jamshid Hashimi, CodeWeekend

Agenda

• What is C#?• Why C#?• History of C#• What is Object?• Object Oriented Programming• Managed Languages• C# Syntax• Installing C#• Downloading and Installing Visual Studio

What is C#

• C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java.• Designed by: Microsoft• Developed by: Microsoft• First Appeared at: 2000, 15 years ago• Stable Release: 5.0• Development Team Lead by: Anders Hejlsberg

Why C#?

• Modernized Language• C# is a modernized version of C++• It was C, then C++. Now it C#

• Type-safety• You can not use uninitialized variables• You can not walk past the end of an array

• Object Oriented• C# goes to another level, even simple data types can be treated as Objects.

• Simplified Syntax• C++ is powerful but not easy. C# is type safe, for example C# does away with Pointers. C# do not allow direct

memory manipulation, so pointers are not needed.• Header files have also been removed from C#. The namespace and reference operators, :: and -> respectively,

have been replaced with a single operator, the period (.).• int and bool data types are completely different now!• C# removes memory management issues by using .NET garbage collection scheme.

Why C#?

• XML Comments• Comments can be source code independent

• Not Just Microsoft• Mono

• The power to be unsafe• You can directly access to memory• If you want to drive without a seatbelt, you are free to do so.

• Is a general purpose language• Dev Productivity

History of C#

• January 1999: Anders Hejlsberg formed a team to build a new language, named Cool, which stood for “C-like Object Oriented Language”.• July 2000, PDC: Language name changed to C#• Anders Hejlsberg:

• C# Principle designer and lead architect• Also designed Turbo Pascal and Delphi• Stated: Flaws in other languages (e.g. C++, Java, Delphi, and Smalltalk) drove the

fundamentals of CLR, which, in return drove the design of C#.• “It is not a clone of Java, it is much closer to C++ in its design”

• To many programmers, C# is Microsoft’s answer to Java.

History of C#

• The name• Inspired by musical notation where a sharp indicates that the written note

should made a semitone higher in pitch. • C++, means C incremented and C#, means C++ incremented. (# is made by

four +)

What is Object?

• Object is the foundational piece of object oriented programming language.• An object typically makes a concept. • An object is something e.g. customer• An object has data e.g. customer’s first name• An object performs action

• Make a customer preferred• Color a car• Change customer’s name

• Object is a thing• To define a thing we need to talk about its characteristics.

What is Object?

• Suppose you are a Personnel Manager for a company and need to hire someone in an important position.• You select one resume, Jane• You are Jack• You call her• Arrange an interview, she will fly to your location• You two never met before, so started asking few questions from each other: height, hair type,

cloth color and briefcase color• Without realizing it, you used objects in the course of your conversation• You created a Person class. A class is a template used to describe an object.• A class is an abstraction or simplification of some object your observe in the real world.• Objects has two basic components:

• Properties that describe the object• Methods, or actions, that you want to associate with the object

What is Object?

Object Oriented Programming

• To be object oriented, a language is designed around the concept of objects. • Objects have certain properties as exhibits certain behaviors.• It means language generally includes support for:• Encapsulation• Inheritance• Polymorphism

Object Oriented Programming

• Encapsulation• The wall around code

• I don’t want you to get confused• I just want you get in touch with me with the Windows and Doors.

• A customer has a first name, but you don’t care how that’s stored. You just want to get in and extract the information needed. • We encapsulate to keep our code secure.

Object Oriented Programming

• Inheritance• Fundamentally based on the idea of code reuse. • You create behaviors, others can use it through inheritance channel.• The quality you get from your parents

Object Oriented Programming

Object Oriented Programming

• Polymorphism• from Greek πολύς, polys, "many, much" and μορφή, morphē, "form, shape"• Objects can appear in different shapes, forms.• Instead of sticking with the concept of “many shapes”, perhaps the definition should be

amended to mean “many messages.”• In essence Polymorphism means that you can send the same message to a group of

different classes and that each class will know how to respond correctly to that message.• clsBuilding – base class, RemoveSnow() - virtual• clsApartment – RemoveSnow() - override• clsCommercial – RemoveSnow() – override• myApt.RemoveSnow()• myComm.RemoveSnow()• myHome.RemoveSnow() – We don’t have a RemoveSnow method for our clsHome object. Still it

outputs.

Managed Languages

• Managed language depends on services provided by a runtime environment• C# is one of many programming languages which it compiles into managed

code. (others are: F#, C++, VB and more..)

• Managed runtime in .NET is called Common Language Runtime (CLR)• Common Language Runtime (CLR) provides:• Automatic Memory Management• Exception Handling• Standard Types• Security

Managed Languages

• Managed Code• Managed code is a code that has its execution managed by .NET framework

Common Language Runtime (CLR)• When you compile C# code to .exe, it is compiled to Common Intermediate

Language (CIL), bytecode. • Whenever you run a CIL executable, it is executed on Microsoft Common

Language Runtime (CLR) virtual machine.• You must have the .NET runtime installed on any client machines where your

program will be running.• C# do not compiles directly to machine code. It compiles to CIL (bytecode).

CLR executes the bytecode to machine code.

C# Syntax

• C# syntax is based on C and C++ syntax• Identifiers are names of classes, methods, variables and so on• Console, Writeline, AcceptDetails(), GetArea(), length, width, Display and so

on.

• Keywords are compiler reserved words• Public, class, string, get, set, void and so on.

• Using keyword is used to include namespaces inside our program.

C# Syntax

C# Syntax

C# Syntax

Thank You!