+ All Categories
Home > Documents > cwpfl03-utilityp1-140211141153-phpapp02

cwpfl03-utilityp1-140211141153-phpapp02

Date post: 02-Jun-2018
Category:
Upload: win-der-lome
View: 217 times
Download: 0 times
Share this document with a friend

of 92

Transcript
  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    1/92

    Mohammad Shaker

    mohammadshaker.com

    C# Programming Course

    @ZGTRShaker

    2011, 2012, 2013, 2014

    C# StarterL03 Utilities

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    2/92

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    3/92

    const and readonly

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    4/92

    const and readonly

    const

    Must be initialized in the time of declaration

    readonly

    keyword is a modifier that you can use on fields. When a field declaration inclua readonly modifier, assignments to the fields introduced by the declaration capart of the declaration or in a constructor in the same class.

    public const int WEIGHT = 1000;

    public readonly int y = 5;

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    5/92

    const and readonly

    const

    Must be initialized in the time of declaration

    readonly

    keyword is a modifier that you can use on fields. When a field declaration inclua readonly modifier, assignments to the fields introduced by the declaration capart of the declaration or in a constructor in the same class.

    public const int WEIGHT = 1000;

    public readonly int y = 5;

    public readonly int y = 5;

    public readonly int y;

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    6/92

    const and readonly

    const

    Must be initialized in the time of declaration

    readonly

    keyword is a modifier that you can use on fields. When a field declaration inclua readonly modifier, assignments to the fields introduced by the declaration capart of the declaration or in a constructor in the same class.

    public const int WEIGHT = 1000;

    public readonly int y = 5;

    public readonly int y = 5;

    public readonly int y;

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    7/92

    Casting

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    8/92

    Casting

    Direct Casting as Keyword

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    9/92

    void Example(object o){

    // Casting

    string s = (string)o; // 1 (Direct Casting)

    // -OR-

    string s = o as string; // 2 (Casting with as Keyword)

    // -OR-

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Whats the difference? Output?

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    10/92

    void Example(object o){

    // Casting

    string s = (string)o; // 1 (Direct Casting)

    // -OR-

    string s = o as string; // 2 (Casting with as Keyword)

    // -OR-

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Whats the difference? Output?

    Number (1) If (o is not a string) Throws InvalidCastException if o is not a string. Otherwise, assigns o to s, even if o is null.

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    11/92

    void Example(object o){

    // Casting

    string s = (string)o; // 1 (Direct Casting)

    // -OR-

    string s = o as string; // 2 (Casting with as Keyword)

    // -OR-

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Whats the difference? Output?

    Number (2) Assigns null to s if o is not a string or if o is null. Otherwise assign to s CANT BE A NULL VALUEFor this reason, you cannot use it with value types.

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    12/92

    void Example(object o){

    // Casting

    string s = (string)o; // 1 (Direct Casting)

    // -OR-

    string s = o as string; // 2 (Casting with as Keyword)

    // -OR-

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Whats the difference? Output?

    Number(3) Causes a NullReferenceException of o is null. Assigns whatever o.ToString() returns to s, no matter what type o is.

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    13/92

    void Example(object o){

    // Casting

    string s = (string)o; // 1 (Direct Casting)

    // -OR-

    string s = o as string; // 2 (Casting with as Keyword)

    // -OR-

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Whats the difference? Output?

    1. Throws InvalidCastException if o is not a string. Otherwise, assigns o to s, even if o is null.2. Assigns null to s if o is not a string or if o is null. Otherwise, assigns o to s. For this reason, you can

    value types.3. Causes a NullReferenceException of o is null. Assigns whatever o.ToString() returns to s, no matter

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    14/92

    void Example(object o){

    // I swear o is a string, C# compiler! Im ordering you to cast, I know what am

    string s = (string)o; // 1 (Direct Casting)

    // I Donna know whether o is a string or not, try a cast and tell me!

    string s = o as string; // 2 (Casting with as Keyword)

    // Am programming badly with this code!

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Meaning!

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    15/92

    void Example(object o){

    // I swear o is a string, C# compiler! Im ordering you to cast, I know what am

    string s = (string)o; // 1 (Direct Casting)

    // I Donna know whether o is a string or not, try a cast and tell me!

    string s = o as string; // 2 (Casting with as Keyword)

    // Am programming badly with this code!

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Meaning!

    as casting will never throw an exception, while Direct cast can.

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    16/92

    void Example(object o){

    // I swear o is a string, C# compiler! Im ordering you to cast, I know what am

    string s = (string)o; // 1 (Direct Casting)

    // I Donna know whether o is a string or not, try a cast and tell me!

    string s = o as string; // 2 (Casting with as Keyword)

    // Am programming badly with this code!

    string s = o.ToString(); // 3 Calling a method

    }

    Casting

    Meaning!

    as casting will never throw an exception, while Direct cast can.

    as cannot be used with value types (non-nullable types).

    using System;

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    17/92

    g y ;

    class MyClass1{}

    class MyClass2{}

    public class IsTest

    {

    public static void Main()

    { object[] myObjects = new object[6];

    myObjects[0] = new MyClass1();

    myObjects[1] = new MyClass2();

    myObjects[2] = "hello";

    myObjects[3] = 123;

    myObjects[4] = 123.4;

    myObjects[5] = null;

    for (int i = 0; i < myObjects.Length; ++i){

    string s = myObjects[i] as string;

    Console.Write("{0}:", i);

    if (s!= null)

    Console.WriteLine("'" + s + "'");

    else

    Console.WriteLine("not a string");

    }

    }}

    Casting

    using System;

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    18/92

    g y

    class MyClass1{}

    class MyClass2{}

    public class IsTest

    {

    public static void Main()

    { object[] myObjects = new object[6];

    myObjects[0] = new MyClass1();

    myObjects[1] = new MyClass2();

    myObjects[2] = "hello";

    myObjects[3] = 123;

    myObjects[4] = 123.4;

    myObjects[5] = null;

    for (int i = 0; i < myObjects.Length; ++i){

    string s = myObjects[i] as string;

    Console.Write("{0}:", i);

    if (s!= null)

    Console.WriteLine("'" + s + "'");

    else

    Console.WriteLine("not a string");

    }

    }}

    Casting

    0:not a st

    1:not a st

    2:'hello'

    3:not a st

    4:not a st

    5:not a st

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    19/92

    is Keyword

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    20/92

    is Keyword

    The is operator is used to check whether the run-time type of an ocompatible with a given type.

    The is operator is used in an expression of the form:

    expression is type

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    21/92

    is Keyword

    Lets have the following example

    class Class1{}

    class Class2{}

    public class IsTest{

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    22/92

    is Keyword{

    public static void Test(object o)

    {

    Class1 a;

    Class2 b;

    if (o is Class1)

    {

    Console.WriteLine("o is Class1");

    a = (Class1)o;

    // do something with a}

    else if (o is Class2)

    {

    Console.WriteLine("o is Class2");

    b = (Class2)o;

    // do something with b

    }

    else

    {

    Console.WriteLine("o is neither Class1 nor Class2.");

    }}

    public static void Main()

    {

    Class1 c1 = new Class1();

    Class2 c2 = new Class2();

    Test(c1);

    Test(c2);

    Test("a string");

    }

    }

    d

    public class IsTest{

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    23/92

    is Keyword{

    public static void Test(object o)

    {

    Class1 a;

    Class2 b;

    if (o is Class1)

    {

    Console.WriteLine("o is Class1");

    a = (Class1)o;

    // do something with a}

    else if (o is Class2)

    {

    Console.WriteLine("o is Class2");

    b = (Class2)o;

    // do something with b

    }

    else

    {

    Console.WriteLine("o is neither Class1 nor Class2.");

    }}

    public static void Main()

    {

    Class1 c1 = new Class1();

    Class2 c2 = new Class2();

    Test(c1);

    Test(c2);

    Test("a string");

    }

    }

    o is Class1

    o is Class2

    o is neither Class1

    Press any key to co

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    24/92

    enum

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    25/92

    enum

    As easy as:

    And thats it!

    // You define enumerations outside of other classes.

    public enum DaysOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

    DaysOfWeek dayOfWeek = DaysOfWeek.Monday;

    if(dayOfWeek == Wednesday)

    {// Do something here

    }

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    26/92

    Exception Handling

    E i Hi h

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    27/92

    Exception Hierarchy

    E ti H dli

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    28/92

    Exception Handling

    using System;

    using System.IO;

    class tryCatchDemo

    {static void Main(string[] args)

    {

    try

    {

    File.OpenRead("NonExistentFile");

    }

    catch(Exception ex)

    {

    Console.WriteLine(ex.ToString());

    }}

    }

    The output?

    E ti H dli

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    29/92

    Exception Handling

    using System;

    using System.IO;

    class tryCatchDemo

    {static void Main(string[] args)

    {

    try

    {

    File.OpenRead("NonExistentFile");

    }

    catch(Exception ex)

    {

    Console.WriteLine(ex.ToString());

    }}

    }

    The output?

    E ti H dli

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    30/92

    Exception Handling

    using System;

    using System.IO;

    class tryCatchDemo

    {static void Main(string[] args)

    {

    try

    {

    File.OpenRead("NonExistentFile");

    }

    catch(Exception ex)

    {

    Console.WriteLine(ex.ToString());

    }

    }

    }

    The output?

    Exception Handling Multiple catchs

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    31/92

    Exception Handling Multiple catch s

    try

    {

    int number = Convert.ToInt32(userInput);

    }

    catch (FormatException e)

    {

    Console.WriteLine("You must enter a number.");

    }

    catch (OverflowException e)

    {

    Console.WriteLine("Enter a smaller number.");

    }

    catch (Exception e)

    {

    Console.WriteLine("An unknown error occurred.");

    }

    Exception Handling

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    32/92

    Exception Handling

    Multiple catch statement?!

    catch(FileNotFoundException e)

    {

    Console.WriteLine(e.ToString());

    }

    catch(Exception ex)

    {

    Console.WriteLine(ex.ToString());

    }

    Exception Handling finally keyword

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    33/92

    Exception Handling finally keywordclass FinallyDemo

    {

    static void Main(string[] args)

    {

    FileStream outStream = null;

    FileStream inStream = null;

    try

    {

    outStream = File.OpenWrite("DestinationFile.txt");

    inStream = File.OpenRead("BogusInputFile.txt");

    }

    catch(Exception ex)

    {

    Console.WriteLine(ex.ToString());

    }

    finally

    {

    if (outStream!= null)

    {outStream.Close();

    Console.WriteLine("outStream closed.");

    }

    if (inStream!= null)

    {

    inStream.Close();

    Console.WriteLine("inStream closed.");

    }

    }

    }

    }

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    34/92

    Throwing Exceptions

    Throwing Exceptions

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    35/92

    Throwing Exceptions

    Like this:

    But why?

    public void CauseProblemsForTheWorld() //always up to no good...

    {

    throw new Exception("Just doing my job!");}

    Searching for a catch

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    36/92

    Searching for a catch

    Caller chain is traversed backwards until a method with a matching cfound.

    If none is found => Program is aborted with a stack trace

    Exceptions don't have to be caught in C (in contrast to Java)

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    37/92

    Creating Exceptions

    Creating Exceptions

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    38/92

    Creating Exceptions

    Lets throw an exception when you over-eat Hamburgers!

    Now you can say:

    public class AteTooManyHamburgersException : Exception

    {

    public int HamburgersEaten { get; set; }

    public AteTooManyHamburgersException(int hamburgersEaten)

    {

    HamburgersEaten = hamburgersEaten;

    }

    }

    try

    {

    EatSomeHamburgers(32);

    }

    catch(AteTooManyHamburgersException hamburgerException)

    {

    Console.WriteLine(hamburgerException.HamburgersEaten + " is too many hamburgers.");

    }

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    39/92

    Unsafe Code!

    Unsafe Code!

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    40/92

    Unsafe Code!

    unsafe code == (code using explicit pointers and memory allocation

    // The following fixed statement pins the location of

    // the src and dst objects in memory so that they will

    // not be moved by garbage collection.

    fixed (byte* pSrc = src, pDst = dst)

    {

    byte* ps = pSrc;

    byte* pd = pDst;

    // Loop over the count in blocks of 4 bytes, copying an

    // integer (4 bytes) at a time:for (int n =0; n < count/4; n++)

    {

    *((int*)pd) = *((int*)ps);

    pd += 4;

    ps += 4;

    }

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    41/92

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    42/92

    Nullable Types - The Concept

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    43/92

    Nullable Types

    Declaration

    DateTime? startDate;

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    44/92

    Nullable Types

    Declaration

    DateTime? startDate;

    // You can assign a normal value to startDate like this:

    startDate = DateTime.Now;

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    45/92

    Nullable Types

    Declaration

    DateTime? startDate;

    // You can assign a normal value to startDate like this:

    startDate = DateTime.Now;

    // or you can assign null, like this:

    startDate = null;

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    46/92

    yp

    Declaration

    // Here's another example that declares and initializes a

    nullable int:

    int? unitsInStock = 5;

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    47/92

    yp

    class Program

    {

    static void Main()

    {

    DateTime? startDate = DateTime.Now;

    bool isNull = startDate == null;

    Console.WriteLine("isNull: " + isNull);

    }

    }

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    48/92

    yp

    class Program

    {

    static void Main()

    {

    DateTime? startDate = DateTime.Now;

    bool isNull = startDate == null;

    Console.WriteLine("isNull: " + isNull);

    }

    }

    isNull: False

    Press any key to continue...

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    49/92

    yp

    class Program

    {

    static void Main()

    {

    int i = null;

    }

    }

    Nullable Types

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    50/92

    yp

    class Program

    {

    static void Main()

    {

    int i = null;

    }

    }

    Compiler error, Cant convert from null to type int

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    51/92

    using keywordusing Directive - using statem

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    52/92

    using Directive

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    53/92

    The using Directive has two uses:

    To permit the use of types in a namespace so you do not have to qualify the uthat namespace:

    To create an alias for a namespace or a type:

    using System.Text;

    using Project = PC.MyCompany.Project;

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    54/92

    namespace PC

    {

    // Define an alias for the nested namespace.

    using Project = PC.MyCompany.Project;

    class A{

    void M()

    {

    // Use the alias

    Project.MyClass mc = new Project.MyClass();

    }

    }

    namespace MyCompany

    {namespace Project

    {

    public class MyClass { }

    }

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    55/92

    namespace PC

    {

    // Define an alias for the nested namespace.

    using Project = PC.MyCompany.Project;

    class A{

    void M()

    {

    // Use the alias

    Project.MyClass mc = new Project.MyClass();

    }

    }

    namespace MyCompany

    {namespace Project

    {

    public class MyClass { }

    }

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    56/92

    namespace PC

    {

    // Define an alias for the nested namespace.

    using Project = PC.MyCompany.Project;

    class A{

    void M()

    {

    // Use the alias

    Project.MyClass mc = new Project.MyClass();

    }

    }

    namespace MyCompany

    {namespace Project

    {

    public class MyClass { }

    }

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    57/92

    namespace PC

    {

    // Define an alias for the nested namespace.

    using Project = PC.MyCompany.Project;

    class A{

    void M()

    {

    // Use the alias

    Project.MyClass mc = new Project.MyClass();

    }

    }

    namespace MyCompany

    {namespace Project

    {

    public class MyClass { }

    }

    }

    }

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    58/92

    using statement

    using statement

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    59/92

    C#, through the.NET Framework common language runtime (CLR), areleases the memory used to store objects that are no longer require

    The release of memory is non-deterministic; memory is released whCLR decides to perform garbage collection. However, it is usually beslimited resources such as file handles and network connections as qupossible.

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    60/92

    Defines a scope, outside of which an object or objects will be dispose

    using (Font font1 = new Font("Arial", 10.0f))

    {// Use font1

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    61/92

    Defines a scope, outside of which an object or objects will be dispose

    using (Font font1 = new Font("Arial", 10.0f))

    {// Use font1

    }

    using (Font font3 = new Font("Arial", 10.0f),

    font4 = new Font("Arial", 10.0f)){

    // Use font3 and font4.

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    62/92

    class C : IDisposable

    {

    public void UseLimitedResource()

    {

    Console.WriteLine("Using limited resource...");

    }

    void IDisposable.Dispose()

    {

    Console.WriteLine("Disposing limited resource.");

    }

    }

    class Program

    { static void Main()

    {

    using (C c = new C())

    {

    c.UseLimitedResource();

    }

    Console.WriteLine("Now outside using statement.");

    Console.ReadLine();

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    63/92

    class C : IDisposable

    {

    public void UseLimitedResource()

    {

    Console.WriteLine("Using limited resource...");

    }

    void IDisposable.Dispose()

    {

    Console.WriteLine("Disposing limited resource.");

    }

    }

    class Program

    { static void Main()

    {

    using (C c = new C())

    {

    c.UseLimitedResource();

    }

    Console.WriteLine("Now outside using statement.");

    Console.ReadLine();

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    64/92

    class C : IDisposable

    {

    public void UseLimitedResource()

    {

    Console.WriteLine("Using limited resource...");

    }

    void IDisposable.Dispose()

    {

    Console.WriteLine("Disposing limited resource.");

    }

    }

    class Program

    { static void Main()

    {

    using (C c = new C())

    {

    c.UseLimitedResource();

    }

    Console.WriteLine("Now outside using statement.");

    Console.ReadLine();

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    65/92

    class C : IDisposable

    {

    public void UseLimitedResource()

    {

    Console.WriteLine("Using limited resource...");

    }

    void IDisposable.Dispose()

    {

    Console.WriteLine("Disposing limited resource.");

    }

    }

    class Program

    { static void Main()

    {

    using (C c = new C())

    {

    c.UseLimitedResource();

    }

    Console.WriteLine("Now outside using statement.");

    Console.ReadLine();

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    66/92

    class C : IDisposable

    {

    public void UseLimitedResource()

    {

    Console.WriteLine("Using limited resource...");

    }

    void IDisposable.Dispose()

    {

    Console.WriteLine("Disposing limited resource.");

    }

    }

    class Program

    {

    static void Main()

    {

    using (C c = new C())

    {

    c.UseLimitedResource();

    }

    Console.WriteLine("Now outside using statement.");

    Console.ReadLine();

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    67/92

    class C : IDisposable

    {

    public void UseLimitedResource()

    {

    Console.WriteLine("Using limited resource...");

    }

    void IDisposable.Dispose()

    {

    Console.WriteLine("Disposing limited resource.");

    }

    }

    class Program

    {

    static void Main()

    {

    using (C c = new C())

    {

    c.UseLimitedResource();

    }

    Console.WriteLine("Now outside using statement.");

    Console.ReadLine();

    }

    }

    using Directive

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    68/92

    class C : IDisposable

    {

    public void UseLimitedResource()

    {

    Console.WriteLine("Using limited resource...");

    }

    void IDisposable.Dispose()

    {

    Console.WriteLine("Disposing limited resource.");

    }

    }

    class Program

    {

    static void Main()

    {

    using (C c = new C())

    {

    c.UseLimitedResource();

    }

    Console.WriteLine("Now outside using statement.");

    Console.ReadLine();

    }

    }

    Using limited resource...

    Disposing limited resource

    Now outside using stateme

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    69/92

    Type Aliases

    Type Aliases

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    70/92

    For instant naming we can use this for long type names:

    And then just using it as follow

    using SB = System.Text.StringBuilder;

    SB stringBuilder = new SB("InitialValue");

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    71/92

    Files

    Files, One Shot Reading

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    72/92

    // Change the file path here to where you want it.

    String path = @C:/Users/Mhd/Desktop/test1.txt;

    string fileContents = File.ReadAllText(path);

    string[] fileContentsByLine = File.ReadAllLines(path);

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    73/92

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    74/92

    FilesReading and Writing on a Text-based Fil

    Files

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    75/92

    StreamWriter

    Object for writing a stream down

    StreamReader

    Object for reading a stream up

    using System;

    using System.IO;

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    76/92

    public partial class Test

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    // Create a file to write to.

    using (StreamWriter sw = new StreamWriter(path))

    {

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    // Open the file to read from.

    using (StreamReader sr = new StreamReader(path))

    {

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    using System;

    using System.IO;

    Hello

    And

    Welcome

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    77/92

    public partial class Test

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    // Create a file to write to.

    using (StreamWriter sw = new StreamWriter(path))

    {

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    // Open the file to read from.

    using (StreamReader sr = new StreamReader(path))

    {

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    Press any key to c

    using System;

    using System.IO;

    bli ti l l T t

    Hello

    And

    Welcome

    P k t

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    78/92

    public partial class Test

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    // Create a file to write to.

    using (StreamWriter sw = new StreamWriter(path))

    {

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    // Open the file to read from.

    using (StreamReader sr = new StreamReader(path))

    {

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    Press any key to c

    using System;

    using System.IO;

    class Test

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    79/92

    class Test

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    if (!File.Exists(path))

    {// Create a file to write to.

    using (StreamWriter sw = File.CreateText(path))

    {

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    }

    // Open the file to read from.

    using (StreamReader sr = File.OpenText(path))

    {

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    using System;

    using System.IO;

    class Test

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    80/92

    class Test

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    if (!File.Exists(path))

    {// Create a file to write to.

    using (StreamWriter sw = File.CreateText(path))

    {

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    }

    // Open the file to read from.

    using (StreamReader sr = File.OpenText(path))

    {

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    using System;

    using System.IO;

    class Test

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    81/92

    class Test

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    if (!File.Exists(path))

    {// Create a file to write to.

    using (StreamWriter sw = File.CreateText(path))

    {

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    }

    // Open the file to read from.

    using (StreamReader sr = File.OpenText(path))

    {

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    using System;

    using System.IO;

    class Test

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    82/92

    class Test

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    if (!File.Exists(path))

    {// Create a file to write to.

    using (StreamWriter sw = File.CreateText(path))

    {

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    }

    // Open the file to read from.

    using (StreamReader sr = File.OpenText(path))

    {

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    using System;

    using System.IO;

    class Test

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    83/92

    {

    public static void Main()

    {

    string path = @"c:\temp\MyTest.txt";

    if (!File.Exists(path))

    {// Create a file to write to.

    using (StreamWriter sw = File.CreateText(path)){

    sw.WriteLine("Hello");

    sw.WriteLine("And");

    sw.WriteLine("Welcome");

    }

    }

    // Open the file to read from.

    using (StreamReader sr = File.OpenText(path)){

    string s = "";

    while ((s = sr.ReadLine())!= null)

    {

    Console.WriteLine(s);

    }

    }

    }

    }

    Reading and Writing on a Binary Files

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    84/92

    Reading and Writing on a Binary FilesBinaryWriter and BinaryReader

    // Change the file path here to where you want it.

    String path = @C:/Users/Mhd/Desktop/test1.txt;

    FileStream fileStream = File.OpenWrite(path);

    BinaryWriter binaryWriter = new BinaryWriter(fileStream);

    binaryWriter.Write(2);

    binaryWriter.Write("Hello");

    binaryWriter.Flush();binaryWriter.Close();

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    85/92

    Class Diagram

    Class Diagram

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    86/92

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    87/92

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    88/92

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    89/92

    Windows Forms

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    90/92

    Windows Forms

  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    91/92

    To learn more about Windows Forms,visit my C++.NET course @

    http://www.slideshare.net/ZGTRZGTR/exactly the same as C#

    http://www.slideshare.net/ZGTRZGTR/http://www.slideshare.net/ZGTRZGTR/
  • 8/10/2019 cwpfl03-utilityp1-140211141153-phpapp02

    92/92

    Thats it for today!


Recommended