Professional Documents
Culture Documents
Day 2
Interface Properties Indexers Collections Introduction to Preprocessor directive Compiler Switches Assemblies
Private Public Versioning
Interface
Definition: An interface is a collection of abstract methods that is used to define the functionality of an application. The methods of an interface are defined in the classes or structs that implement the interface.
Interface
Uses of Interface: Interfaces can be used as a means to provide some inheritance features with structures. Multiple interfaces can be implemented with a class, thus gaining functionality one cant obtain with an abstract class.
Interface
Syntax: Interface_ Modifer interface Interface_ identifier : base_interface E.g.: public interface ITextBox : IControl { . } // interface body
The interface body contains the members of the interface. An interface contains the members that are inherited from its base interfaces and its own members.
Interface
Members of an interface:
Methods Properties Events
An interface modifier cannot be specified for an interface member. All the interface members are public by default. This is because any class or struct in the program can access the interface members. Interface members are defined in a class. An interface cannot contain constants, fields and operators. An interface cannot contain constructors and destructors.
class Circle : IShape { void Draw() { // interface method definition // method definition
. } }
Interface
Copyright 2005, 7 Infosys Technologies Ltd
Interface
E.g.: interface ICntrl { void Draw(); } interface ITxtBox : ICntrl { void SetText( string text); } class TxtBox : ITxtBox { void ICntrl.Draw() { } // method definition // method definition // method declaration // interface method declaration
void ITxtBox.SetText() { } }
If an inherited interface is implemented in a class, the class implicitly implements the base interface members of the inherited interface.
class TextBox : IDelete, ITransact { void IDelete.Delete() { ... } // method definition void ITransact.Delete() { ... } // method definition }
// method body
// method body
Which statement gives a compilation error in the code snippet listed below: interface IAdd { void } Add ( int a, int b);
Spot the error in the code snippet listed below: interface IAdd { void } Add ( int a, int b); // method declaration
class Calculate : IAdd { void Add ( int a, float b) { // method definition . } } // method body
Properties
C# provides a concept called properties to enable to create object oriented fields within the classes. Properties use the keywords get and set to get the values from the variables and set the values in the variables.
class point { int iPointx; // iPointx is private int iPointy; // iPointy is private public int iPointx1 { get { return iPointx; }
set { iPointx = value; } } public int iPointy1 { get { return iPointy; } set { iPointy = value; } } }
Class MyApp { public static void Main() { point starting = new point(); point ending = new point(); starting.iPointx1 = 1; starting.iPointy1 = 4; ending.iPointx1 = 10; ending.iPointy1 = 11; System.Console.WriteLine(Point 1: {0},{1}), starting.iPointx1, starting.iPointy1); System.Console.WriteLine(Point 2: {0},{1}), ending.iPointx1, ending.iPointy1); } }
Properties - Demo
Properties (Class1.cs)
Properties (Class2.cs)
Indexers
Enables an object to be indexed in the same way as an array. The signature of indexer is <modifier> <return type> this [argument list] The modifier can be private, public, protected or internal The this is a special keyword in C# to indicate the object of the current class The formal-argument-list specifies the parameters of the indexer. C# do not have the concept of static indexers A base class indexer is inherited to the derived class.
I d x r(Cl
cs)
I d x r(Cl ss2.cs)
I d x r
An indexer is identified by its signature. An indexer is always an instance member An indexer is accessed through an element access.
Pre-processing directives
These directives are called preprocessor directives because before compiling the code, the compiler preprocesses the listing and evaluates any preprocessor directives.
Directive
Description
#define, #undef
#error, #warning
Cl
// preprocess.cs using the preprocessor directives // --------------------------------------------------------#define DEBUG Using System; Using System.IO; Public class ReadingApp { public static void Main(String[] args) { if( args.Length < 1) { Console.WriteLine(Must include file name.); } else {
Copyright 2005, 22 Infosys Technologies Ltd ER/CORP/CRS/LA30FC/003 Version no: 2.0
#if DEBUG Console.WriteLine( ========== DEBUG INFO========== ); for ( int x= 0; x < args.Length ; x++) { Console.WriteLine( Arg[{0}] = {1}, x, args[x]); } Console.WriteLine( =============================== ); #endif string buffer; StreamReader myFile = File.OpenText(args[0]); while ( buffer = myfile.ReadLine()) != null ) { #if DEBUG Console.Write( {0:D3} , buffer.Length); #endif Console.WriteLine(buffer); } myFile.Close(); } } }
Copyright 2005, 23 Infosys Technologies Ltd ER/CORP/CRS/LA30FC/003 Version no: 2.0
Collection
A collection is a specialized class that organizes and exposes a group of objects. Various collection classes are ArrayList, BitArray, HashTable, SortedList, Queue and Stack They are all included in System.Collections namespace Like arrays, members of collections can be accessed by an index. Unlike arrays, collections can be resized dynamically. Collection Classes implement following interfaces IEnumerable - Supports method GetEnumerator () IEnumerator - Supports method MoveNext () and property Current
ArrayList
System.Collections.ArrayList class allows to dynamically add and remove items from a simple list. The list of objects managed by the Array List is a zero based collection. The items in the list are retrieved by accessing the item index. Methods
Add() Remove() RemoveAt()
Property
Indexer Count
Arr yL st
Hashtable
System.Collections.Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses hashing algorithm to store and index values It makes use of the GetHashCode() method to generate the hashcode Methods
Add() Remove()
Property
Indexer Count
HashTable
foreach loop
Can be used to loop through in an array or collection in turn. int [] num= {10,20,30,40}; foreach (int i in num) { Console.WriteLine(i); } //Assumes that myList is an ArrayList that contains Strings //and variety of objects of other types foreach (object o in myList) { if(o.GetType() == typeof(string)){ Console.WriteLine(o.ToString()); } }
Copyright 2005, 27 Infosys Technologies Ltd ER/CORP/CRS/LA30FC/003 Version no: 2.0
Compile Switches
-option or /option /doc:file.xml
Used to process documentation comments Are generated after compiling source file and stored in a xml file csc /doc:firstc.xml firstc.cs
/out:filename
Can specify the name of the output file csc /out:firstprogram.exe firstc.cs
Compile Switches
Error report /bugreport:file
Can generate an error report, all the compilation errors are stored in the file csc /bugreport:error.txt firstc#.cs
Contains
Copy of all course code files in the compilation List of compiler option used in the compilation Version information about the compiler, run time and operating system Output generated by the compiler
Assemblies
Assemblies are fundamental building blocks of a .NET Framework application. They contain the types and resources that make up an application and describe those contained types to the CLR. An assembly consists of four internal parts:
Assembly Manifest or Metadata Type Metadata IL code Resource files
TestDLL
My ssembly
Test ssembly
Strong Naming
A strong name is a name that guarantees an assembly identity. Consists of - Name of assembly - Version number - Public key of public/private key pair A strong name can be generated with sn.exe
Versioning
Step 1: In AssemblyInfo.cs of TestLib change Assembly Version to 2.0.0.0 Step 2 : In .cs file,change in multiply() return x*y to 2*x*y Step 3 : Rebuild the class library and publish again using gacutil. Step 4: Recompile TestClient application , it now uses Version 2.0. Step 5 : Verify at C:\Windows\assembly , two entries for TestLib exists.
Configuration File
Step 1: Open TestClient.sln. Step 2: From Project Menu, selct Add - > New Item -> Application Configuration File and click on Open. App.config is added. Step 3 : Click on Start->All Programs -> Microsoft .NET SDK FrameWork -> Documentation. Step 4 : Look for bindingredirect Step 5: From the given example copy and paste the entire contents between <configuration></configuration> in App.config.
Configuration File
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="myAssembly" publicKeyToken="32ab4ba45e0a69a1" culture="neutral" /> <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
Configuration File
Step 6: Change assembly name to TestLib . Step 7 : Change public key token. For it thru Windows Explorer, move to location c:\Windows\assembly and select TestLib. Step 8: Right Click on TestLib and select Properties.Copy and Paste public key token in App.config file. Step 9 : Rebuild application. Run TestClient.exe from the .NET command prompt and path C:\SharedAssembly\TestClient\bin\debug. Version 2.0 will be used. Step 10: In order to use,Version 1.0,open App.config and interchange old and new version. Step 11 : Rebuild application. Run TestClient.exe from the .NET command prompt and path C:\SharedAssembly\TestClient\bin\debug. Version 1.0 will be used.
Summary
Interface Properties Indexers Collections Introduction to Preprocessor directive Compiler Switches Assemblies
Private Public Versioning
Thank You!
Copyright 2005, 47 Infosys Technologies Ltd ER/CORP/CRS/LA30FC/003 Version no: 2.0