Search This Blog

Thursday, November 7, 2013

Sealed modifier C#

C# > Modifiers > sealed

Sealed modifier prevents other classes from inheriting from it.
Also you can use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.

Examples

1. Sealed class
    class C1 { }
    sealed class C2 : C1 { }
    class C3 : C2 { } // Error : cannot derive from sealed type C2

2. Sealed method
    class A
    {
        protected virtual void F() { }
        protected virtual void F1() { }
    }
    class B : A
    {
        sealed protected override void F() { }
        protected override void F1() { }
    }
    class C : B
    {
        protected override void F() { } //Error cannot override inherited member because it is sealeD
        protected override void F1() { }
    }



const c#

C# > Modifiers > const

The const specifies that the value of the field or the local variable is constant and it cannot be modified.
 
The const keyword differs from the readonly keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor.

Example:

class SampleConst
    {
        public int x;
        public int y;
        public const int c1 = 1;
        public const int c2 = c1 + 5;

        public SampleConst(int v1)
        {
            x = v1 + c1;
            y = x + c2;
        }
    }

Wednesday, November 6, 2013

sbyte keyword C#

C# > Types > sbyte

The sbyte keyword is an integral type.
Range: -128 to 127
Size: Signed 8-bit integer

Example:

sbyte k = 127; // ok
k = 128;      // Error     1      Constant value '128' cannot be converted to a 'sbyte' 





int keyword C#

C# > Types > int

The int keyword is an integral type.
Range: -2,147,483,648 to 2,147,483,647
Size: Signed 32-bit integer

Example:

int i = 100;

int x = 3.0; // Error 1 Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

int y = (int)3.0; // OK: explicit conversion.





Stopwatch C# Example

C# > Diagnostics > Stopwatch 

Stopwatch measures elapsed time.

The Stopwatch measures elapsed time by counting timer ticks in the timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time.

Example:


Stopwatch sw = new Stopwatch();
sw.Start();
Thread.Sleep(500);
sw.Stop();
TimeSpan ts = sw.Elapsed;
MessageBox.Show("Elapsed time " + ts.ToString());





C# operators

C# > Operators

C# operators are symbols that specify which operations to perform in an expression.










InteropServices C#

C# > InteropServices

InteropServices namespace provides members that support COM interop and platform invoke services.

The .NET Framework allows interaction with COM components, COM+ services and external type libraries.
Managed code executes under the control of the runtime.
Unmanaged code runs outside the runtime. COM components, ActiveX interfaces, and Win32 API functions are examples of unmanaged code.