C Fundamentals An Introduction Before we begin How
C# Fundamentals An Introduction
Before we begin • How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5. 0 – C# 6. 0 , Visual Studio. NET 2015
. NET YOUR APPLICATION Common Language Runtime (CLR) Framework Class Library (FCL) Microsoft. NET
Framework Class Library • A library of functionality to build applications
C# • One of the languages for. Net – Create applications, services and reusable libraries – Syntax is very similar to java, C++, Javascript
Types • Classes – A class may be composed of any number of members (such as constructors, properties, methods, and events) and data points (fields). In C#, classes are declared using the class keyword • Enums – Enumerations are a handy programming construct that allow you to group name/value pairs. For example, assume you are creating a video game application that allows the player to select one of three character categories (Wizard, Fighter, or Thief). Rather than keeping track of simple numerical values to represent each possibility, you could build a strongly typed enumeration using the enum keyword.
• Structs – a structure can be thought of as a lightweight class type having value-based semantics. Typically, structures are best suited for modeling geometric and mathematical data and are created in C# using the struct keyword. • Interface – Interfaces are nothing more than a named collection of abstract member definitions, which may be supported (i. e. , implemented) by a given class or structure. In C#, interface types are defined using the interface keyword. • Delegates
Statements • The actions that a program takes are expressed in statements. Common actions include declaring variables, assigning values, calling methods, looping through collections, and branching to one or another block of code, depending on a given condition.
Types of statements in C# • Declaration statements – A declaration statement introduces a new variable. • Expression statements – Expression statements that calculate a value must store the value in a variable. – // Expression statement (new object creation). System. Collections. Generic. List<string> strings = new System. Collections. Generic. List<string>(); • Selection statements – Selection statements enable you to branch to different sections of code, depending on one or more specified conditions. (if, else, switch, case) • Iteration statements – Iteration statements enable you to loop through collections like arrays, or perform the same set of statements repeatedly until a specified condition is met. (do, foreach, in, while) • Exception handling statements – Exception handling statements enable you to gracefully recover from exceptional conditions that occur at run time. (throw, try-catch, try-finally, try -catch-finally)
OOPS • Inheritance • Abstraction • Encapsulation • Polymorphism
Encapsulation • According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that are not intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits.
Members All methods, fields, constants, properties, and events must be declared within a type; these are called the members of the type • The following list includes all the various kinds of members that may be declared in a class or struct. • Fields • Constants • Properties • Methods • Constructors • Events • Indexers • Nested Types
Access Modifiers • Access modifiers are keywords used to specify the declared accessibility of a member or a type. • public : Access is not restricted. • protected : Access is limited to the containing class or types derived from the containing class. • Internal : Access is limited to the current assembly. • protected internal: Access is limited to the current assembly or types derived from the containing class. • private : Access is limited to the containing type.
Polymorphism • At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object's declared type is no longer identical to its run-time type. • Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation.
Types of Polymorphism • Compile Time Polymorphism – We declare methods with same name but different signatures because of this we will perform different tasks with same method name. • Run Time Polymorphism – Example
Delegates • A Type Safe Function Pointer • A delegate is a references type that invokes single/multiple method(s) through the delegate instance. It holds a reference of the methods. • A Delegate is similar to class. You can create an instance of it, and pass in the function name as a parameter it points to.
Types Of Delegates • Single Delegate • Multicast Delegate • Generic Delegate – Action – Predicate – Func
Anonymous methods • Anonymous method is a method without a name. They provide us a way of creating delegate instances without having to write a separate method.
- Slides: 18