Introduction

37
INTRODUCTION BEGINNING C#

description

Introduction. Beginning C# . C# and the .NET runtime and libraries. The C# compiler compiles and convert C # programs. NET Common Language Runtime (CLR ) executes Managed versus unmanaged languages. .net base class library ( bcl ). Performing network operations - PowerPoint PPT Presentation

Transcript of Introduction

Page 1: Introduction

INTRODUCTIONBEGINNING C#

Page 2: Introduction

C# AND THE .NET RUNTIME AND LIBRARIES• The C# compiler compiles and convert C# programs.• NET Common Language Runtime (CLR) executes • Managed versus unmanaged languages

Page 3: Introduction

.NET BASE CLASS LIBRARY (BCL)

• Performing network operations • Performing I/O operations• Managing security• Globalizing programs • Manipulating text

Page 4: Introduction

• Accessing a database • Manipulating XML • Interacting with event logging, tracing, and other diagnostic

operations • Using unmanaged code • Creating and calling code dynamically

Page 5: Introduction

SPECIALIZED LIBRARIES ON TOP OF BCL

• Console applications • Windows GUI applications, using either Windows Forms or the

Windows Presentation • Foundation (WPF)• ASP.NET (web) applications

Page 6: Introduction

• Windows Services • Service-oriented applications, using Windows Communication

Foundation (WCF) • Workflow-enabled applications, Windows Workflow

Foundation (WF) • Windows 8 applications • Windows Phone applications

Page 7: Introduction

C# QUICKSTARTusing System;class Hello{

public static void Main(string[] args){

Console.WriteLine("Hello, Universe");// iterate over command-line arguments,// and print them outfor (int arg = 0; arg < args.Length; arg++){

Console.WriteLine("Arg {0}: {1}", arg, args[arg]);}

}}

Page 8: Introduction

NAMESPACE AND USING STATEMENTS

• Namespaces in the .NET Runtime are used to organize classes and other types into a single hierarchical structure• Can be nested

Page 9: Introduction

namespace Outer{

namespace Inner{class MyClass{public static void Function() {}}}

}

Page 10: Introduction

namespace Outer.Inner{

class MyClass{

public static void Function() {}}

}

Page 11: Introduction

USING CLASSES

• ClassSystem.Xml.Serialization.Advanced.SchemaImporterExtension

• With the statement usingusing System.Xml.Serialization.Advanced;

• Calling the classSchemaImporterExtension

Page 12: Introduction

WRONG CALL

• This will not work using System.Xml.Serialization;

• we would not be able to use the following name:Advanced.SchemaImporterExtension

Page 13: Introduction

VARIANT

using ThatConsoleClass = System.Console;class Hello{

public static void Main(){ThatConsoleClass.WriteLine("Hello");}

}

Page 14: Introduction

NAMESPACES AND ASSEMBLIES

• The compiler will open the single assembly known as mscorlib.dll• An object can be used from within a C# source file only if

that object can be located by the C# compiler.• Use the option /r:<assembly> in command line to reference

other assemblies• Ex: System.Net types and child namespaces reside in the

System.Net.dll assembly

Page 15: Introduction

BASIC DATA TYPES

Page 16: Introduction

DATA TYPES

• The difference between the built-in data types and user-defined data types is that it is possible to write literal values for the built-in types.• Data types are separated into value types and reference

types. • Value types are either stack allocated or allocated inline in a structure. • Reference types are heap allocated.

Page 17: Introduction

BOXING AND UNBOXING

• Both reference and value types are derived from the ultimate base class object. In cases where a value type needs to act like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type’s value is copied into it. • This process is known as boxing, and the reverse process is known

as unboxing.• Boxing and unboxing let you treat any type as an object.

Page 18: Introduction

BOXING EXAMPLE

using System;class Hello{

public static void Main(string[] args){Console.WriteLine("Value is: {0}", 3);}

}

Page 19: Introduction

CLASSES, STRUCTS AND INTERFACES

• Class keyword is used to declare a reference (a heap-allocated) type• struct keyword is used to declare a value type

• C# and the .NET Runtime do not support multiple inheritance for classes but do support multiple implementation of interfaces.

Page 20: Introduction

STATEMENTS

• Similar with C++• foreach – iterate over arrays and collections• lock – used for mutual exclusion in threading scenarios• checked and unchecked – are used for control overflow in

arithmetic operations and conversions

Page 21: Introduction

ENUMS

• Used to declare a set of related constantsenum Colors{

red,green,blue

}

Page 22: Introduction

DELEGATES AND EVENTS

• Delegates are a type-safe, object-oriented implementation of function pointers and are used in many situations where a component needs to call back to the component that is using it. • Used as the basis for events, which allows a delegate to

easily be registered for an event.• To be discussed in the future.

Page 23: Introduction

PROPERTIES AND INDEXERS

• C# supports properties and indexers, which are useful for separating the interface of an object from the implementation of the object

Page 24: Introduction
Page 25: Introduction

ATTRIBUTES

• Attributes are used in C# and the .NET Frameworks to communicate declarative information from the writer of the code to other code that is interested in the information (• Used to specify which fields of an object should be serialized, what transaction

context to use when running an object, how to marshal fields to native functions, or how to display a class in a class browser.• Attributes are specified within square braces. A typical attribute usage might

look like this:• [CodeReview("12/31/1999", Comment = "Well done")]

Page 26: Introduction

CLASSES 101

Page 27: Introduction

A SIMPLE CLASS

Page 28: Introduction
Page 29: Introduction

MEMBER FUNCTIONS

Page 30: Introduction

REF AND OUT PARAMETERS

• The ref  (out) method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method.• It is not possible to define an overload that only differs by ref

and out.

Page 31: Introduction

REF EXAMPLE

using System;public class MyClass { public static void TestRef(ref char i) { // The value of i will be changed in the calling method i = 'b'; } public static void TestNoRef(char i) { // The value of i will be unchanged in the calling method i = 'c'; }

// This method passes a variable as a ref parameter; the value of the // variable is changed after control passes back to this method. // The same variable is passed as a value parameter; the value of the // variable is unchanged after control is passed back to this method. public static void Main() { char i = 'a'; // variable must be initialized TestRef(ref i); // the arg must be passed as ref Console.WriteLine(i); TestNoRef(i); Console.WriteLine(i); }}

Page 32: Introduction

• Outputb

b

Page 33: Introduction

OUT EXAMPLE

using System;public class MyClass { public static int TestOut(out char i) { i = 'b'; return -1; }

public static void Main() { char i; // variable need not be initialized Console.WriteLine(TestOut(out i)); Console.WriteLine(i); }}

• Output-1b

Page 34: Introduction

OVERLOADING

Page 35: Introduction

VISUAL STUDIO 2012

Page 36: Introduction
Page 37: Introduction

• CRTL + F5 - without debugging• F5• c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

Program.cs