h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Set the process priority

                   Set the process priority, we  can set the Process.PriorityClass property. PriorityClass is captured in the ProcessPriorityClass enumeration, which lets us set the process priority to Idle, Normal, High, AboveNormal, BelowNormal, or RealTime.   Process.BasePriority is an integer value property and is read-only.
The following table shows the relationship between the BasePriority and PriorityClass values:
S.No BasePriority PriorityClass
1 4 Idle.
2 8 Normal.
3 13 High.
4 24 RealTime.

Note:
In Windows 98, and the Windows Millennium Edition Platform, setting the priority class to AboveNormal or BelowNormal causes an exception to be thrown.
How do I capture a screenshot of a process’s main window?
Here is one method to take a process screenshot.  We will use the process “notepad” for this example.   Please follow these steps:
1.   Use System.Diagnostics.Process.MainWindowHandle to get the process information we require to take a screenshot.
2.   Use the Windows API, SetForegroundWindow, to activate the target process.
3.   Pass the window handle to the first parameter of the Windows API GetWindowRect, to get the location information of the target process.
4.   Define the coordinates of the process window on the screen with the location information in step 3.
5.   Use the coordinates in step 4 and Graphics.CopyFromScreen to take the screenshot.

Object Initializers and Collection Initializers

Object Initializers
Object initializers provide a way to assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.
Object initializers with named types
Here we use auto-implemented properties featured in Visual C# 3.0 to define a class.  For details on auto-implemented properties, please check Auto-Implemented Properties (C# Programming Guide).
public class Point
{
    public int X { get; set; }
    public int Y { get; set; }
}
When we instantiate the objects of a Point class, we can use:
Point p = new Point();
p.X = 0;
p.Y = 1;
In Visual C# 3.0, there is a short way to achieve the same results:
// {X = 0, Y = 1} is field or property assignments
Point p = new Point { X = 0, Y = 1 };
In LINQ, we can use named object initializer like this:
The following example shows how we can use named object initializer with LINQ.  The example assumes that an object contains many fields and methods related to a product, but we are only interested in creating a sequence of objects that contain the product name and the unit price.
 var productInfos =
      from p in products
      select new { ProductName = p.ProductName, UnitPrice = p.UnitPrice };
Collection Initializers
Collection Initializers are similar in concept to Object Initializers.  They allow you to create and initialize a collection in one step.  By using a collection initializer you do not have to specify multiple calls to the Add method of the class in your source code; the compiler adds the calls.
List<int> numbers = new List<int> { 1, 100, 100 };
In fact, it is the short form of the following:
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(10);
numbers.Add(100);
Note: To be able to use a Collection Initializer on an object, the object must satisfy these two requirements:

  •          It must implement the IEnumerable interface.
  •          It must have a public Add() method.


Implicit type var and Anonymous Types

Beginning in Visual C# 3.0, variables that are declared in the method scope can have an implicit type var.  We can use the modifier var to instruct the compiler to infer and assign the type, as shown below:
var i = 23;      // int i = 23;
var s = "Hello"; // string s = "Hello";
Arrays can also be declared with implicit typing as shown below:
var a = new[] { 1, 2, 3, 4 }; // int[]
var b = new[] { "hello", null, "world" }; // string[]
var c = new[] { a, new[] { 5, 6, 7, 8 } }; // single-dimension jagged array

Note: The following restrictions apply to implicitly-typed variable declarations:
  • var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null literal, because it does not have a type – like lambda expressions or method groups.  However, it can be initialized with an expression that happens to have the value null, as long as the expression has a type.
  • var cannot be used on fields in class scope.
  • Variables declared by using var cannot be used in their own initialization expression.  
  • In other words, var v = v++; will result in a compile-time error.
  • Multiple implicitly-typed variables cannot be initialized in the same statement.
  • If a type named var is in scope, then we will get a compile-time error if we attempt to initialize a local variable with the var keyword.

Anonymous Type
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type.  The type name is generated by the compiler and is not available at the source code level.  The type of the properties is inferred by the compiler.  The following example shows an anonymous type being initialized with two properties called Amount and Message:
var v = new { Amount = 123, Message = "Hello" };
Anonymous types are class types that consist of one or more public read-only properties.   No other kinds of class members such as methods or events are allowed.
Note: Some rules on anonymous types:
  •  You must provide a name to a property that is being initialized with an expression, except in the situation that the second bullet describes.
  • If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the variable, field or property being used to initialize them.    
  • Anonymous types are limited to a local scope.


Can base/derived classes be exported to COM


  •  COM only deals with interfaces.   Base/derived classes have no meaning or functionality in COM.   Inheritance is not applicable either.
  • In COM, interfaces can inherit from one another.   However, the .NET implementation that exports the .NET interface to COM does not support inheritance.   Therefore, you must replicate any interface members in a base interface to the derived interface.
  • Moving members between a base and derived class will have no impact on what is visible to COM.
  • Only the programmer can define what is exposed to COM.   The complier will not use reflection or anything else to determine what should be exposed.
  • All COM classes have a single, default interface.   This is the interface that is normally used for an object.   A COM class can expose other interfaces but the COM client must then query for the interface.   In .NET the first COM visible interface is used as the default interface for a COM class. 


Difference between Object and Dynamic

object
This keyword is nothing more than a shortcut for System.Object, which is the root type in the C# class hierarchy
Here is a short example that demonstrates some of the benefits and problems of using the object keyword.
object obj = 10;
Console.WriteLine(obj.GetType());
obj = (int)obj + 10;
Dynamic
The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).
dynamic dyn = 10;
Console.WriteLine(dyn.GetType());
dyn = dyn + 10;
dyn = 10.0;
dyn = dyn + 10;
dyn = "10";
dyn = dyn + 10;
This is one of the main differences between object and dynamic –
with dynamic you tell the compiler that the type of an object can be known only at run time, and the compiler doesn’t try to interfere.
As a result, you can write less code.
I want to emphasize that this is no more dangerous than using the original object keyword. However, it is not less dangerous either, so all the type-checking techniques that you need to use when operating with objects (such as reflection) have to be used for dynamic objects as well.
Refer : http://blogs.msdn.com/b/csharpfaq/archive/2010/01/25/what-is-the-difference-between-dynamic-and-object-keywords.aspx

Define C#.net Advantages


  • XML documentation generated from source code comments. (This is coming in VB.NET with Whidbey (the code name for the next version of Visual Studio and .NET), and there are tools which will do it with existing VB.NET code already.)
  • Operator overloading - again, coming to VB.NET in Whidbey.
  • Language support for unsigned types (you can use them from VB.NET, but they aren't in the language itself). Again, support for these is coming to VB.NET in Whidbey.
  • The using statement, which makes unmanaged resource disposal simple.
  • Explicit interface implementation, where an interface which is already implemented in a base class can be reimplemented separately in a derived class. Arguably this makes the class harder to understand, in the same way that member hiding normally does.
  • Unsafe code. This allows pointer arithmetic etc, and can improve performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies). Note that unsafe code is still managed code, i.e. it is compiled to IL, JITted, and run within the CLR.


Define VB.net Advantages


  • Support for optional parameters - very handy for some COM interoperability
  • Support for late binding with Option Strict off - type safety at compile time goes out of the window, but legacy libraries which don't have strongly typed interfaces become easier to use.
  • Support for named indexers (aka properties with parameters).
  • Various legacy VB functions (provided in the Microsoft.VisualBasic namespace, and can be used by other languages with a reference to the Microsoft.VisualBasic.dll). Many of these can be harmful to performance if used unwisely, however, and many people believe they should be avoided for the most part.
  • The with construct: it's a matter of debate as to whether this is an advantage or not, but it's certainly a difference.
  • Simpler (in expression - perhaps more complicated in understanding) event handling, where a method can declare that it handles an event, rather than the handler having to be set up in code.
  • The ability to implement interfaces with methods of different names. (Arguably this makes it harder to find the implementation of an interface, however.)
  • Catch ... When ... clauses, which allow exceptions to be filtered based on runtime expressions rather than just by type.
  • The VB.NET part of Visual Studio .NET compiles your code in the background. While this is considered an advantage for small projects, people creating very large projects have found that the IDE slows down considerably as the project gets larger.

use an alias for a namespace or class

Use the using directive to create an alias for a long namespace or class name. You can then use it anywhere you normally would have used that class or namespace. The using alias has a scope within the namespace you declare it in. Sample


// Namespace:
using act = System.Runtime.Remoting.Activation;
// Class:
using list = System.Collections.ArrayList;
...
list l = new list(); // Creates an ArrayList
act.UrlAttribute foo; // Equivalent to System.Runtime.Remoting.Activation.UrlAttribute foo

Simple Threading in C#.net

Today ,we will discuss about small threading program in C#.Net

using System;
using System.Threading;

class ThreadTest
{
    public void Run()
    {
        Console.WriteLine("Run Called");
        Thread.Sleep(10000);
    }

    public static void Main(String[] args)
    {
        ThreadTest b = new ThreadTest();
        Thread t = new Thread(new ThreadStart(b.Run));
        t.Start();

        Console.WriteLine("Thread 't' started.");
        Console.WriteLine("There is no telling when " +
                          "'Run' will be invoked. ");

        t.Join();

        Console.WriteLine("Thread 't' has ended.");
    }
}