C Course Material0209


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C Course Material0209

  1. 1. C#.NET
  2. 2. Week 1 <ul><li>What is .NET Framework? </li></ul><ul><li>It is kind of environment in which a developer can develop almost any kind of application. For ex </li></ul><ul><li>Console Applications </li></ul><ul><li>Windows Applications </li></ul><ul><li>Database Applications </li></ul><ul><li>Web Applications </li></ul><ul><li>Windows & Web Services </li></ul><ul><li>XML Web Services </li></ul>
  3. 3. Week 1 <ul><li>Different Versions of .NET Framework </li></ul><ul><li>.NET Framework 1.0 (VS 2002) </li></ul><ul><li>.NET Framework 1.1 (VS 2003) </li></ul><ul><li>.NET Framework 2.0 (VS 2005) </li></ul><ul><li>.NET Framework 3.0 (VS 2008) </li></ul><ul><li>.NET Framework 3.5 (VS 2008) </li></ul><ul><li>.NET Framework 4.0 (VS 2010) </li></ul>
  4. 4. Week 1 <ul><li>.NET Framework Architecture </li></ul><ul><li>The framework is contains so many components. One of the most important is CLR (Common Language Runtime) </li></ul>
  5. 5. <ul><li>Common Language Runtime </li></ul><ul><li>In the central of .NET Framework its runtime execution environment, responsible for execution of any .NET application. </li></ul><ul><li>Before execution by CLR any source code compile in two steps: </li></ul><ul><li>Compilation of source code in IL Code </li></ul><ul><li>Compilation of IL code to platform specific code by CLR </li></ul>Source Code in .NET Compatible Language Language Compiler IL Intermediate language Code Twice Compile with JIT (Just in time compiler) Native Machine Code
  6. 6. Week1 <ul><li>What is IL or MSIL Code and what are its advantages? </li></ul><ul><li>Microsoft intermediate language shares with Java byte code the idea that it is a low-level language with a simple syntax (based on numeric codes rather than text), which can be very quickly translated into native machine code. Having this well-defined universal syntax for code has significant advantages </li></ul><ul><li>Advantages of IL </li></ul><ul><li>Platform Independence </li></ul><ul><li>Performance Improvement </li></ul><ul><li>Language interoperability </li></ul>
  7. 7. Week 1 <ul><li>Platform Independence </li></ul><ul><li>Platform Independence means a application having capability to run on different-2 machine and different-2 environment. </li></ul><ul><li>It means that the same file containing byte code instructions can be placed on any platform; at runtime the final stage of compilation can then be easily accomplished so that the code will run on that particular platform. So on particular platform we need JIT, it is in CLR, it is in Framework ie it necessary to have .NET framework for execution of any .NET application. </li></ul><ul><li>In other words, by compiling to IL we obtain platform independence for .NET, in much the same way as compiling to Java byte code gives Java platform independence . </li></ul><ul><li>You should note that the platform independence of .NET is only theoretical at present because, at the time of writing, a complete implementation of .NET is only available for Windows. However, there is a partial implementation available </li></ul>
  8. 8. Week 1 <ul><li>Performance Improvement </li></ul><ul><li>Although we previously made comparisons with Java, IL is actually a bit more ambitious than Java byte code. IL is always Just-In-Time compiled (known as JIT compilation), whereas Java byte code was often interpreted. </li></ul><ul><li>One of the disadvantages of Java was that, on execution, the process of translating from Java byte code to native executable resulted in a loss of performance (with the exception of more recent cases, where Java is JIT compiled on certain platforms). </li></ul><ul><li>Instead of compiling the entire application in one go (which could lead to a slow start-up time), the JIT compiler simply compiles each portion of code as it is called (just-in-time). When code has been compiled once, the resultant native executable is stored until the application exits, so that it does not need to be recompiled the next time that portion of code is run </li></ul>
  9. 9. Week 1 <ul><li>Language Interoperability </li></ul><ul><li>In .NET framework we can use more than one language (near about 64 languages) so the unique feature of .NET is that we can inter operate the different-2 language code. </li></ul><ul><li>The use of IL not only enables platform independence; it also facilitates language interoperability . Simply put, you can compile to IL from one language, and this compiled code should then be interoperable with code that has been compiled to IL from another language. </li></ul>
  10. 10. <ul><li>Till now we have seen .NET framework supports more than one languages and the language will compile to IL, since any language </li></ul><ul><li>that targets .NET would logically need to support the main characteristics of IL too. </li></ul><ul><li>Here are the important features of IL: </li></ul><ul><li>Object-orientation and use of interfaces </li></ul><ul><li>Strong distinction between value and reference types </li></ul><ul><li>Strong data typing </li></ul><ul><li>Error handling through the use of exceptions </li></ul><ul><li>Use of attributes </li></ul><ul><li>Let’s now take a closer look at each of these characteristics. </li></ul>
  11. 11. <ul><li>Object-orientation and use of interfaces </li></ul><ul><li>The particular route that Microsoft has chosen to follow for IL is that of classic object-oriented programming, with single implementation inheritance of classes . </li></ul><ul><li>Strong distinction between value and reference types </li></ul><ul><li>As with any programming language, IL provides a number of predefined primitive data types. One characteristic of IL, however, is that it makes a strong distinction between value and reference types. </li></ul><ul><li>Value types are those for which a variable directly stores its data, while reference types are those for which a variable simply stores the address at which the corresponding data can be found. </li></ul>Specifications of IL
  12. 12. <ul><li>Strong data typing </li></ul><ul><li>What we mean by strong data typing is that all variables are clearly marked as being of a particular, specific data type (there is no room in IL, for example, for the Variant data type recognized by Visual Basic and scripting languages). In particular, IL does not normally permit any operations that result in ambiguous data types. </li></ul><ul><li>Although enforcing type safety might initially appear to hurt performance, in many cases the benefits gained from the services provided by .NET that rely on type safety far outweigh this performance loss. Such services include: </li></ul><ul><li>Language interoperability </li></ul><ul><li>Garbage collection </li></ul><ul><li>Security </li></ul>Specifications of IL
  13. 13. <ul><li>The Importance of strong data typing for language interoperability </li></ul><ul><li>This data type problem is solved in .NET through the use of the Common Type System (CTS). The CTS defines the predefined data types that are available in IL, so that all languages that target the .NET Framework will produce compiled code that is ultimately based on these types. </li></ul><ul><li>For the example that we were considering before, Visual Basic .NET’s Integer is actually a 32-bit signed integer, which maps exactly to the IL type known as Int32. This will therefore be the data type specified in the IL code. Because the C# compiler is aware of this type, there is no problem. At source code level, C# refers to Int32 with the keyword int, so the compiler will simply treat the Visual Basic .NET method as if it returned an int. </li></ul>Specifications of IL
  14. 14. <ul><li>C# is specifically design to work with .NET framework it is having its own programming elements </li></ul><ul><li>KeyWords </li></ul><ul><li>Predefine words in the language for e.g. void, for, delegate, class etc. </li></ul><ul><li>Identifier </li></ul><ul><li>It is the name of the programming unit, may be name of variable, constant, namespace, class, method, property etc. </li></ul><ul><li>Ii follows two naming styles in C# </li></ul><ul><li>i) PascalCasing ii) camelCasing </li></ul><ul><li>Literals </li></ul><ul><li>Any constant value refer as literal may be numeric or alphabetic. </li></ul><ul><li>13, 23.45 are numeric constant, “India” is string constant </li></ul><ul><li>Operator </li></ul><ul><li>Predefine symbols in the language, used to perform certain actions </li></ul><ul><li>for e.g. +, %, *….. etc </li></ul>Week1
  15. 15. <ul><li>Variables </li></ul><ul><li>Variable are the named stored location in memory whose contents can be change during program execution. </li></ul><ul><li>Data Type </li></ul><ul><li>Represent what kind of value the variable will store. </li></ul><ul><li>types </li></ul><ul><li>user define predefine </li></ul><ul><li>value type reference type </li></ul>10 n This Value can be change
  16. 16. <ul><li>Syntax for declaration </li></ul><ul><li>datatype variablename ; </li></ul><ul><li>In C# program you declare the variable at following places </li></ul><ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><li>private int a; // Field level variable </li></ul><ul><li>public void M1() </li></ul><ul><li>{ </li></ul><ul><li>int b; // local variable for a method </li></ul><ul><li>for(int i=0;i<10;i++) // local variable for a block </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Variable Declaration, Scope and default value of Variable
  17. 17. <ul><li>Scope of variable </li></ul><ul><li>The accessibility & availability of a variable in a program is called scope of variable. </li></ul><ul><li>In C# program variable may have three kind of scope </li></ul><ul><li>Field level scope </li></ul><ul><li>Local scope for a method </li></ul><ul><li>Local scope for a block </li></ul><ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><li>private int a; // Field level Scope </li></ul><ul><li>public void M1() </li></ul><ul><li>{ </li></ul><ul><li>int b; // local scope for a method </li></ul><ul><li>for(int i=0;i<10;i++) // local scope for a block </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Variable Declaration, Scope and default value of Variable
  18. 18. <ul><li>Default value of variable </li></ul><ul><li>In c# field level variable having their default value according to datatype while local variable will not have default or garbage value. For ex. </li></ul><ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><li>private int a; // Field level Scope </li></ul><ul><li>public void M1() </li></ul><ul><li>{ </li></ul><ul><li>int b; // local scope for a method </li></ul><ul><li>Console.WriteLine(a); // default value ie zero </li></ul><ul><li>Console.WriteLine(b); // compilation error </li></ul><ul><li>// compiler will force us to initialize local variable before use </li></ul><ul><li>Console.ReadLine(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Variable Declaration, Scope and default value of Variable
  19. 19. <ul><li>Value Type: </li></ul><ul><li> Value types are those for which a variable directly stores its data, </li></ul><ul><li>int i=10; // value type </li></ul><ul><li>int j= i; </li></ul><ul><li>here i & j both are the value type. </li></ul><ul><li>Reference Type: </li></ul><ul><li>Reference types are those for which a variable simply stores the reference at which the corresponding data can be found. </li></ul><ul><li>Test t1; </li></ul>10 i 10 j
  20. 20. <ul><li>Example of reference type </li></ul><ul><li>Test t1; //reference type t1 is created and it can refer any object of /// class Test type. </li></ul><ul><li>t1= new Test(); </li></ul><ul><li>Test t2; </li></ul><ul><li>t2=t1 // only reference transforms </li></ul>Object of Test class t1 t2
  21. 21. Type Casting <ul><li>Boxing: </li></ul><ul><li>When Value type is converted into reference type then it is called boxing. </li></ul><ul><li>int n=10; </li></ul><ul><li>object ob = (object)n; // Boxing </li></ul><ul><li>Unboxing: </li></ul><ul><li>When boxed object again converted into value type than it is called unboxing. </li></ul><ul><li> int j = (int)ob; </li></ul>
  22. 22. <ul><li>Memory organization: Stack & Heap concepts </li></ul><ul><li>In c# program memory divided into logical regions, stack and heap area </li></ul><ul><li>Value types and reference allocated memory in stack area while reference type gets memory in heap area. </li></ul>i j t1 t2 stack heap
  23. 23. <ul><li>types </li></ul><ul><li>Predefine User define </li></ul><ul><li>Value type Reference type class struct </li></ul><ul><li>(Ref type) (value type) </li></ul><ul><li>string object </li></ul><ul><li>sbyte short int long byte ushort uint ulong </li></ul><ul><li>float double decimal </li></ul><ul><li>char </li></ul><ul><li>bool </li></ul>Data Types
  24. 24. Console Input/Output <ul><li>Input: we have Console.ReadLine() method for taking input from console </li></ul><ul><li>This method will return string. </li></ul><ul><li>Output : For output we have Console.WriteLine() method. </li></ul><ul><li>The use of these two methods expalin with help of simple program </li></ul><ul><li>namespcae ConsoleIO </li></ul><ul><li>{ </li></ul><ul><li>class InputOutput </li></ul><ul><li>{ </li></ul><ul><li>public static void Main() </li></ul><ul><li>{ </li></ul><ul><li>int a, b; </li></ul><ul><li>//input </li></ul><ul><li>Console.WriteLine(“Enter two numbers”); </li></ul><ul><li>a= int.Parse(Console.ReadLine()); // type casting becoz Readline return //string </li></ul><ul><li>b= int.Parse(Console.ReadLine()); </li></ul><ul><li>int c= a+ b; </li></ul><ul><li>// output </li></ul><ul><li>Console.WriteLine(c); </li></ul><ul><li>//or </li></ul><ul><li>Console.WriteLine(“The sum of two numbers==>” +c.ToString()); </li></ul><ul><li>//or </li></ul><ul><li>Console.WriteLine(“The sum of {0} and {1} is {0}”, a,b,c); </li></ul><ul><li>//or </li></ul><ul><li>Console.WriteLine(“The sum of {0,5} and {1,5} is {0,5}”, a,b,c); </li></ul><ul><li>//or </li></ul><ul><li>Console.WriteLine(“The sum of {0,5:c} and {1,5:c} is {0,5:c}”, a,b,c); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  25. 25. Classes and Objects <ul><li>Object </li></ul><ul><li>It is collection of data and its functions. or any real world entity. </li></ul><ul><li>Class </li></ul><ul><li>Class are the way through which we can implement our object. </li></ul><ul><li>or </li></ul><ul><li>Classes are like blue print or template through which you can obtain the information about object. </li></ul><ul><li>or </li></ul><ul><li>Whenever we create a class a new type will be created and it will be reference type. </li></ul><ul><li> </li></ul><ul><li>Object </li></ul>Data Its Functions
  26. 26. Classes and Objects <ul><li>Class Declaration Syntax </li></ul><ul><li>Access Modified class ClassName </li></ul><ul><li>{ </li></ul><ul><li>AccessModifier data1; </li></ul><ul><li>AccessModifier data2; </li></ul><ul><li>…… </li></ul><ul><li>AccessModifier MemberFunction1(); </li></ul><ul><li>AccessModifer MemberFucntion2(); </li></ul><ul><li>… .. </li></ul><ul><li>} </li></ul><ul><li>Access Modifier: Tell where the type and member are available. In C# we are having 5 modifiers </li></ul><ul><li>i) private ii) protected iii) public iv) internal v) protectedinternal </li></ul>
  27. 27. Classes and Objects <ul><li>In a C# class we can write following types of data members and member functions. </li></ul><ul><li> C# Class </li></ul><ul><li>Data Members Member Functions </li></ul><ul><li>Field level variables constant events </li></ul><ul><li>Methods Properties Constructors Finalizer </li></ul>
  28. 28. Classes and Objects <ul><li>Static and Non static (instance) Data Member and member function of the class </li></ul><ul><li>Non static member: </li></ul><ul><li>When we create object than new copy of non static members is created. </li></ul><ul><li>Static Member: </li></ul><ul><li>Once copy of static member will created & it will be share among all the objects. </li></ul><ul><li>for e.g. </li></ul><ul><li>class Test </li></ul><ul><li>{ </li></ul><ul><li>private int a; // non static member </li></ul><ul><li>private static int b; </li></ul><ul><li>} </li></ul><ul><li>Test t1= new Test(); </li></ul><ul><li>Test t2 = new Test(); </li></ul>t1 a t2 a b
  29. 29. Methods in C# <ul><li>Methods: To perform any general operation on data member of the class we can write methods </li></ul><ul><li>Syntax for writing methos </li></ul><ul><li>AccessModifier [static] return type MethodName(argument(s) list) </li></ul><ul><li>{ </li></ul><ul><li>// implementation of methods </li></ul><ul><li>} </li></ul><ul><li>Static and non Methods: </li></ul><ul><li>Static method can access only static members while non static methods can access both types of data members </li></ul><ul><li>Static method can be call without creating object while non static method can be call with object or instance of class only. </li></ul><ul><li>Static method : </li></ul><ul><li>ClassName.MethodName(); </li></ul><ul><li>Non static or instance method : </li></ul><ul><li>object.MethodName(); </li></ul>
  30. 30. Methods in C# <ul><li>Methods Calling Mechanism: </li></ul><ul><li>In c# method can be call with two mechanism </li></ul><ul><li>1. Call by value </li></ul><ul><li>2. Call by reference </li></ul><ul><li>depend on the argument of method </li></ul><ul><li>Method argument </li></ul><ul><li>reference type value type </li></ul><ul><li>Can be call only with can be value or reference mechanism </li></ul><ul><li>reference mechanism By default with value mechanism, for reference mechanism we can use ref keyword </li></ul>
  31. 31. Methods in C# <ul><li>Practical example illustrate method calling mechanism </li></ul><ul><li>using System; </li></ul><ul><li>using System.Collections.Generic; </li></ul><ul><li>using System.Text; </li></ul><ul><li>namespace CallByValueRefEx </li></ul><ul><li>{ </li></ul><ul><li>class Program </li></ul><ul><li>{ </li></ul><ul><li>public static void ChngeData(int[] ar, ref int i) // with ref keyword can also call, use value mechanism </li></ul><ul><li>{ </li></ul><ul><li>i = 100; </li></ul><ul><li>ar[0] = 100; </li></ul><ul><li>} </li></ul><ul><li>static void Main(string[] args) </li></ul><ul><li>{ </li></ul><ul><li>int i = 0; </li></ul><ul><li>int[] a = new int[2]; </li></ul><ul><li>a[0] = 10; </li></ul><ul><li>a[1] = 20; </li></ul><ul><li>Console.WriteLine(&quot;The value of variable and array before calling method are --&quot;); </li></ul><ul><li>Console.WriteLine(&quot;i= {0} nar[0]={1}&quot;, i, a[0]); </li></ul><ul><li>ChngeData(a, ref i); </li></ul><ul><li>Console.WriteLine(&quot;The value of variable and array after calling method are --&quot;); </li></ul><ul><li>Console.WriteLine(&quot;i= {0} nar[0]={1}&quot;, i, a[0]); </li></ul><ul><li>Console.ReadLine(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  32. 32. <ul><li>Property </li></ul><ul><li>It is a special kind of get & set method use to read or write data member. </li></ul><ul><li>Syntax for writing property </li></ul><ul><li>Access Modifier type PropertyName </li></ul><ul><li>{ </li></ul><ul><li>get </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>set </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>To access property: object.PropertyName // get property called </li></ul><ul><li> object.PropertyName=someValue // set Property </li></ul><ul><li>Property may be static or may be non static, in case static can be call with class name. </li></ul>Properties
  33. 33. <ul><li>Example to illustrate the concept of Property </li></ul><ul><li>class Demo </li></ul><ul><li>{ </li></ul><ul><li>private int rollNo; </li></ul><ul><li>// property for above member </li></ul><ul><li>public int RollNo </li></ul><ul><li>{ </li></ul><ul><li>get </li></ul><ul><li>{ </li></ul><ul><li>return rollNO; </li></ul><ul><li>} </li></ul><ul><li>set </li></ul><ul><li>{ </li></ul><ul><li>rollNo=value; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public static void Main() </li></ul><ul><li>{ </li></ul><ul><li>Demo d1= new Demo(); d1.RollNo=134; // set property </li></ul><ul><li>Console.WriteLine(d1.RollNo); // get property </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Properties
  34. 34. <ul><li>Constructor </li></ul><ul><li>It is a special kind of method whose name is same as class name it do not have any return type. </li></ul><ul><li>Default zero argument constructor exist when we not write constructor in the class. </li></ul><ul><li>It will invoke automatically when object is created. </li></ul><ul><li>It may have or may not have argument </li></ul><ul><li>It can be overlaoded. </li></ul><ul><li>Syntax for writing property </li></ul><ul><li>Access Modifier ClassName( [argement(s)] ) </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul>Constructor
  35. 35. <ul><li>this keyword </li></ul><ul><li>It is the common reference for current object. </li></ul><ul><li>Two uses of this keyword </li></ul><ul><li>In the constructor to resolve the scope of class member. </li></ul><ul><li>To call same class constructor in constructor overloading. </li></ul>this keyword
  36. 36. <ul><li>this keyword </li></ul><ul><li>It is the common reference for current object. </li></ul><ul><li>Two uses of this keyword </li></ul><ul><li>In the constructor to resolve the scope of class member. </li></ul><ul><li>To call same class constructor in constructor overloading. </li></ul>Struct in c#
  37. 37. <ul><li>Decision Statements </li></ul><ul><li>This type of statement can be achieve with if..else and switch statement </li></ul><ul><li>Syntax for if statement </li></ul><ul><li>if(condition) </li></ul><ul><li>{ </li></ul><ul><li>s1; // any valid c# statement </li></ul><ul><li>} </li></ul><ul><li>Syntax of if…else statement </li></ul><ul><li>if(condition) </li></ul><ul><li>{ </li></ul><ul><li>s1; s2; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>s3; s4; </li></ul><ul><li>} </li></ul>
  38. 38. <ul><li>Syntax of nested if…else statement </li></ul><ul><li>if(condition1) </li></ul><ul><li>{ </li></ul><ul><li>s1; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>if(condition2) </li></ul><ul><li>{ s2;} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>if(condition3) </li></ul><ul><li>{ </li></ul><ul><li>s4; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>s5; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  39. 39. <ul><li>Switch Statement </li></ul><ul><li>Use to check multiple conditions with only match for equality. </li></ul><ul><li>switch (variable) </li></ul><ul><li>{ </li></ul><ul><li>case value of variable: </li></ul><ul><li>s1; s2; </li></ul><ul><li>break; </li></ul><ul><li>case value of variable: </li></ul><ul><li>s3;s4; </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>s5; </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul>
  40. 40. <ul><li>Iteration statement </li></ul><ul><li>The purpose of these type statement is that we written one time but it will execute more than one time, this can be achieve with the help of loop statement in c# we are having four types of loops </li></ul><ul><li>i) for loop </li></ul><ul><li>ii) while loop </li></ul><ul><li>iii) do while loop </li></ul><ul><li>iv) foreach loop </li></ul>
  41. 41. <ul><li>for loop </li></ul><ul><li>Syntax </li></ul><ul><li>for(initilization of loop variable; condition; increment/ decrement) </li></ul><ul><li>{ </li></ul><ul><li>s1; s2; s3; </li></ul><ul><li>} </li></ul><ul><li>while Loop </li></ul><ul><li>Syntax </li></ul><ul><li>while(condition) </li></ul><ul><li>{ </li></ul><ul><li>s1; s2; s3; </li></ul><ul><li>} </li></ul><ul><li>do while loop </li></ul><ul><li>Syntax do{ </li></ul><ul><li>s1; s2; </li></ul><ul><li>} while (condition); </li></ul>
  42. 42. <ul><li>foreach loop </li></ul><ul><li>This loop is new in c#, it use to iterate items from any collections, without providing conditional statement and increment of decrement statement, its Syntax is </li></ul><ul><li>foreach(local variable in collection) </li></ul><ul><li>{ </li></ul><ul><li>s1; s2; </li></ul><ul><li>} </li></ul>