1 / 37

Introduction

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

bliss
Download Presentation

Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 (intarg = 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:<assembly> 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. 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")]

  25. Classes 101

  26. A Simple class

  27. Member functions

  28. REF AND OUT PARAMETERS • Theref (out) method parameterkeyword 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 andout.

  29. 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); } }

  30. Output b b

  31. Out Example using System; public class MyClass { public static intTestOut(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 -1 b

  32. Overloading

  33. Visual studio 2012

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

More Related