Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.

Similar presentations


Presentation on theme: "INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes."— Presentation transcript:

1 INTRODUCTION BEGINNING C#

2 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

3 .NET BASE CLASS LIBRARY (BCL) Performing network operations Performing I/O operations Managing security Globalizing programs Manipulating text

4 Accessing a database Manipulating XML Interacting with event logging, tracing, and other diagnostic operations Using unmanaged code Creating and calling code dynamically

5 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

6 Windows Services Service-oriented applications, using Windows Communication Foundation (WCF) Workflow-enabled applications, Windows Workflow Foundation (WF) Windows 8 applications Windows Phone applications

7 C# QUICKSTART using System; class Hello { public static void Main(string[] args) { Console.WriteLine("Hello, Universe"); // iterate over command-line arguments, // and print them out for (int arg = 0; arg < args.Length; arg++) { Console.WriteLine("Arg {0}: {1}", arg, args[arg]); }

8 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

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

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

11 USING CLASSES Class System.Xml.Serialization.Advanced.SchemaImporterExtension With the statement using using System.Xml.Serialization.Advanced; Calling the class SchemaImporterExtension

12 WRONG CALL This will not work using System.Xml.Serialization; we would not be able to use the following name: Advanced.SchemaImporterExtension

13 VARIANT using ThatConsoleClass = System.Console; class Hello { public static void Main() { ThatConsoleClass.WriteLine("Hello"); }

14 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: in command line to reference other assemblies Ex: System.Net types and child namespaces reside in the System.Net.dll assembly

15 BASIC DATA TYPES

16 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.

17 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.

18 BOXING EXAMPLE using System; class Hello { public static void Main(string[] args) { Console.WriteLine("Value is: {0}", 3); }

19 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.

20 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

21 ENUMS Used to declare a set of related constants enum Colors { red, green, blue }

22 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.

23 PROPERTIES AND INDEXERS C# supports properties and indexers, which are useful for separating the interface of an object from the implementation of the object

24

25 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")]

26 CLASSES 101

27 A SIMPLE CLASS

28

29 MEMBER FUNCTIONS

30 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.method parameter It is not possible to define an overload that only differs by ref and out.

31 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); }

32 Output b

33 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 b

34 OVERLOADING

35 VISUAL STUDIO 2012

36

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


Download ppt "INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes."

Similar presentations


Ads by Google