Sunteți pe pagina 1din 9

-Pentru a converti un double Tax intr-un format (de ex CURRENCY) intr-un label: labelTax.Text = String.Format({0:C},tax); - double taxableAmount = double.Parse(txtTaxAmount.

Text); -Multi-Dimensional arrays parcurgere: For (int i = 0 ; i < trucks.GetUpperBound(0) + 1 ; i++) For (int j = 0 ; j < trucks.GetUpperBound(1) + 1 ; j++) trucks[i,j]; // prima dimensiune // a doua dimensiune

- CITIRE / SCRIERE FISIERE .TXT


Avem fisierul: test.txt: 1, Bob Tabor, Visual Basic Programmer 2, Dan Starr, C++ Programmer
StreamReader sr = new StreamReader(@"c:\test.txt"); string line; string[] lineValues; ArrayList contents = new ArrayList(); bool finished = false; while (!finished) { line = sr.ReadLine(); if (line == null) { finished = true; } else { lineValues = line.Split(','); //delimitatorul , contents.Add(lineValues); } } // SCRIERE: StreamWriter sw = new StreamWriter(@"c:\test2.txt"); for (int counter = 0; counter < contents.Count; counter++) { lineValues = (string[])contents[counter]; sw.WriteLine(lineValues[0] + " - " + lineValues[1] + " - " + lineValues[2]); }

.NET Framework Components The .NET framework is a collection of classes and the platform that runs your applications. The .NET framework is made up of a number of components. Term Description All .NET programs compile to Microsoft Intermediate Language (MSIL) or IL. Remember these key points about IL:
y y y y

Microsoft Intermediate Language (MSIL)

C# programs are compiled to IL. IL is similar to Java's byte code. IL allows for language interoperability. IL allows platform independence.

The Common Language Runtime (CLR) executes IL code. The CLR is the execution environment for all .NET applications.
y y

Common Language Runtime (CLR)

The CLR is similar to the Java JVM (Java Virtual Machine). The CLR provides a managed environment for code execution. o A managed environment makes the code more secure by protecting the code from doing things, such as illegal memory access operations. o A managed environment manages memory for the program. o A managed environment adds additional runtime support not available in native programs, like garbage collection. The CLR executes IL code. o Note: The CLR uses a Just In Time compiler (JIT) to compile MSIL code to native code. The JIT considerably improves the performance and speed of .NET programs. The JIT is the reason .NET programs are fast. The CLR provides a host of services to .Net applications, including: o Input/Output support. o Multi threading support

Common Type System (CTS)

In order to support language interoperability, the .Net framework contains a Common Type System (CTS). The CTS defines types that are common to all .NET languages. For example, Int32 is a common type. In C# this type is referred to by using the int keyword, while Visual Basic .Net uses the integer keyword. The .NET class libraries are pre-written classes that provide a rich assortment of pre-defined code.

.NET Library

y y

All classes in the .NET libraries are organized into namespaces. Most of the classes in the .NET library can be found in the System namespace.

In .NET, an assembly is the smallest distributable unit.


y y y y

Assembly

Assemblies compile down to either a managed executable (.exe) or a managed dll (.dll). Assemblies contain the IL code pertaining to a dll or executable. Assemblies have a resource file. This file contains resources such as images for the assembly to use. Assemblies have Meta Data about the assembly. o Meta Data stores all of the necessary information to allow other .Net assemblies to interact.

All of the information about an assembly available to the compiler at compile time is kept in the Meta Data.  This allows assemblies to use classes declared in other assemblies.  This information can also be used by the programmer at run time.

Jagged array (gen matrice cu liniile de dimensiuni variabile): int[][] array1 = new int[3][] {new int[3]{1,2,3}, new int[4]{4,5,6,7}, new int[5]{8,9,10,11,12}}; Be aware of the following when working with jagged arrays:
y y

Use brackets [ ] to identify the number of array elements within the jagged array. When creating the array object, only the size of the first rank can be specified when the array is created.

XML Documentation
When you add XML comments just above a function, auto-complete will create stub tags for you to fill out. This allows you to document your code and use the XML features at the same time. Following is an example of XML documentation:

This example uses three XML tags to create the documentation for the function.
y y y

The <summary> tag adds a short description of the function. The <param> tag identifies the parameters that are part of the function. The <returns> tag identifies the value returned by the function.

Later in the code, when you are using this class, intellisense will show your comments:

Notice that the summary of the PowerOf function is displayed by the intellisense (circled in red.) Access modifiers designate restrictions on the members of a class. The access modifiers available in C# are: Modifier private public protected internal protected internal Constructor Method Explanation The member is visible only in the current (or a nested) class. The member is visible to any inheritor or user of the class. The member is visible only to inheritors of the class. The member is visible inside of the assembly (or program). The member is visible inside of the assembly or to inheritors (inside or outside the assembly.) Example class Car : Vehicle { public Car() : base(100) { } } In this example, we modified the implementation of Car so that when its default constructor gets called, instead of calling the Vehicle default constructor, the singleparameter constructor gets called instead. The compiler figures out which constructor to call based upon the parameter signature. class Vehicle { public Vehicle() { // initialization here } Redirect using a peer constructor with the this keyword public Vehicle(int fuelAmount) : this() { // do some other work } } In this example, when Vehicle(int) gets called, the constructor redirects control to Vehicle(). Once Vehicle( ) is finished, control returns to Vehicle(int)and its code body is executed. When you use the this keyword, the compiler figures out which constructor to call based upon the parameters.

Call a specific base constructor using the base keyword

Customizing Inheritance Facts Derived classes inherit the members from the base class. However, you can customize inheritance by using one of the techniques described here. Technique Description Member hiding is the behavior that happens when a derived class has a member with the same name or signature (in the case of a method) as the base class. For example:
class Vehicle { protected int fuelLevel = 100; public int Start(){} } class Boat : Vehicle { private new float fuelLevel = 50.0f; public new int Start(){} }

Hiding Members

In this example the Boat class hides two members of the Vehicle class: the variable fuelLevel and the method Start(). Be aware of the following points about hiding members:
y y y

You can hide any class member including data members, methods, events, and properties. The data member in the derived class that hides the base data member does not need to be of the same type. Use the new keyword in the derived class to alert the compiler that the data member is hidden. Without the new keyword, the compiler will give a warning (not an error) when hiding base class members.

Note: If you reference the hidden member in the derived object, the base class member, not the derived object member, will be referenced. For example:
Vehicle v = new Boat(); v.Start();

The call to v.Start() in this example invokes the Vehicle.Start() method, not the Boat.Start() method. Usually, this is not the behavior you want or expect. When you override a base class member, you write code in the derived class that will be used instead of the code in the base class. To override a member:
y y

In the base class, add the virtual keyword to the member. This allows derived classes to override the member. In the derived class, use the override keyword to redefine the member.

Overriding Members

For example:
class Vehicle { public virtual int Start(){} } class Boat : Vehicle { public override int Start(){}

Given this example, when you access an overridden method as follows:


Vehicle v = new Boat(); v.Start();

The call to v.Start() in this example invokes the Boat.Start() method. This is usually what we want and expect. Contrast this to the hiding example above where the same call accessed the base class member instead. Some things to note about overriding members:
y y y y y

You cannot override data members or a type (such as nested classes, delegates, etc.) You cannot override static members. Private members cannot be virtual. The overriding member must have the same return type and parameters as the base member. You may not change the access modifier when overriding a class member.

Sealing Classes

Sealing a class prevents other classes from inheriting from that class. Use the sealed keyword to prevent inheritance. For example:
public sealed class MySealedClass { }

An abstract class is an incomplete class. Abstract classes are used as non-instantiatable (often base) classes. The following are some of the properties of abstract classes:
y y y y y

Use the abstract keyword to create an abstract class. You may not instantiate the abstract base class. Abstract classes may have (but are not required to have) one or more abstract members. Abstract classes cannot be sealed. As with other classes, when you create a derived class from the abstract class, the derived class inherits the members of the base class.

Methods and properties of an abstract class may be declared as abstract. Abstract members:
y y y

Have no implementation, only a declaration. Must be implemented in any class that inherits directly from the abstract class. Can only be declared in an abstract class.

Abstract Classes

Here is an example of an abstract class with abstract members:


abstract class Shape { // class fields protected int x; protected int y; protected int width; protected int height; // non-abstract method public void SetPosition(int x, int y) { this.x = x; this.y = y; } // abstract method public abstract double CalculateArea();

// abstract properties public abstract Width { get; set; } public abstract Height { get; set; } }

Notice the following in this example:


y y y y

The base class is declared as abstract. The base class has both abstract and normal members. The abstract members contain a definition line, but no code. The derived class is required to add implementation code to the abstract classes. In this example, any derived class must write code which defines the CalculateArea method and the Width and Height properties.

Class - Abstract Class Interface:


Types Contains Public Interface Yes Yes Yes Implementation (Code) Yes Yes/No No Capability Instantiate A class can. . . (Create an object) Yes Inherit (Only 1 class) No No Inherit (Only 1 class) -- Abstract class are generally used as base classes for derived classes. Implement (0 or more interfaces)

Class Abstract Class Interface

Interfaces and abstract classes have some similarities, namely:


y y y

Interfaces must not have any implementation; abstract classes might not have any implementation. You cannot instantiate either an abstract class or an interface. Methods (or properties) that have no implementation in an abstract class or interface must have an implementation in the derived class for abstract classes or the implementing class for interfaces.

Despite these similarities, interfaces and abstract classes have some major differences:
y y

y y y

A class may only inherit from one base, abstract class, but may implement many interfaces. An abstract class may have fields, methods, and properties with implementation. o This means that when a class derived from an abstract class is instantiated, space is allocated for its members. o Space is never allocated for an interface. Abstract classes may have constructors, interfaces may not. Abstract members of an abstract class need not be public. In an interface, all members are public. An abstract class must be derived from a more base class.

DELEGATES
There are four steps when defining and using delegates: 1. 2. 3. 4. Action Declare the delegate Instantiate the delegate Add and remove methods to a delegate (optional) Invoke the delegate Description A new delegate type can be declared in a namespace or a class using the delegate keyword, the return type, and any parameters. The syntax looks like:
// delegate method prototype delegate void MyDelegate(int x, int y);

Declare the delegate

To instantiate a delegate, use the new keyword to create a new delegate variable, then assign a method to the delegate as follows:
class MyClass { // delegate instantiation private MyDelegate myDelegate = new MyDelegate( SomeFun ); // static method public static void SomeFun(int dx, int dy) { } }

Instantiate the delegate

Following are three operations you can perform to modify methods in the invocation list:
// Add a method myDelegate += new MyDelegate( mc.SomeMethod ); // Remove a method myDelegate -= new MyDelegate( mc.SomeMethod ); // Populate the invocation list myDelegate = new MyDelegate( mc.SomeMethod ) + new MyDelegate( mc.SomeOtherMethod );

Be aware of the following about the delegate's invocation list: Add methods to the invocation list
y y y y y y

Delegate methods are executed in the order in which they are added to the invocation list. Add methods with + or +=. Remove methods with - or -=. Attempting to remove a delegate method that does not exist is not an error, it simply does nothing. The return value is whatever the last method in the invocation list returns. A delegate may be present in the invocation list more than once: o The delegate is executed as many times as it appears in the invocation list. o Removing a delegate that is present more than once removes only the last occurrence.

Invoke a delegate just like you would a method: Invoke the delegate
myDelegate(100, 50);

This statement, when included inside a method, will invoke (or call) the method(s) associated with the delegate. Note that if the method being used to instantiate a delegate is an instance method (as opposed to a static method) the method name must be preceded by the variable that holds the instance reference, as the following example demonstrates:
// delegate declaration delegate void MyDelegate(int x, int y); class MainClass { private MyDelegate myDelegate; [STAThread] static void Main() { MainClass mainClass = new MainClass(); // instantiate a delegate using an instance method myDelegate = new MyDelegate( mainClass.MainClassMethod ); } private void MainClassMethod(int x, int y) { public void FireDelegate() { // invoke the delegate if(myDelegate != null) myDelegate(100, 50); } } }

This complete example declares a delegate, instantiates it using an instance method and provides a method that invokes it. Notice that we ensure there is something to invoke (if(myDelegate != null)) before we call the delegate.

S-ar putea să vă placă și