Why Dynamic Programming?
Most C# developers use multiple tools in a single application to accomplish complex tasks. If you’re writing WPF desktop applications, you’re using C# and XAML. It is quite possible that you might find some open source code that solves a problem, but it might be written in another language such as VB or F#. One of the benefits of .NET since its inception is the ability to have cross-language interoperability and the runtime is even called the “Common Language” Runtime (CLR). In recent years, Microsoft has created dynamic languages, such as IronRuby and IronPython, but developers don’t have an easy way to perform interop with dynamic languages. If you have this need, then you’ll welcome the ease with which C# dynamic programming makes interop between C# and dynamic languages possible.
When performing reflection to run a method on an object, there are several hoops to jump through, including obtaining a reference to an object type, getting a reference to a member info object, determining the type of bindings to use, and then invoking the member. While reflection has an undeniable coolness factor, it still feels like a hack and that’s where C# 4.0 dynamic methods can help. Later in this article, I’ll show you how to just call the object member.
Performing COM interop with C# has always been cumbersome; partly because of the need to write extra syntax for conversions, optional parameters, and more. This has left some C# developers with a touch of VB envy because VB has easier COM interop support. One of the purposes of dynamic programming in C# is to help the C# programmer write cleaner syntax in COM interop scenarios.
Interview Questions: What are Properties in .NET, What are the advantages of using Properties in .NET, How to declare a property in .NET?
The primary building block of any .NET object is Data Members. Data members holds data for each objects and thus separates with other members. Properties provide control access to the fields exposed from the class.
Each Properties in .NET is provided with two methods, Get / Set. Get is called whenever the user wants to access the value of the property, while Set is called whenever the value of the property is set.
It is always better to use properties to be exposed publicly from outside rather than public variable. This gives the programmer ability to validate the data before setting it to the member or determine what value to be sent before getting the value. This makes properties in .NET very useful.
Say for instance
// Private variable for the property
private string m_CategoryName = string.Empty;
// Declare the property now
public string CategoryName
{
get { return m_CategoryName; }
set { m_CategoryName = value; }
}
// Private variable for the property
private MyClass mfield = null;
// Declare the property now
public MyClass MyProperty
{
get
{
this.mfield = this.mfield ?? new MyClass(); // Null coalesce
return this.mfield;
}
set
{
if (value == null)
{
//throw exception
}
else this.mfield = value;
}
}
Here mfield is the mapped data element which is set from outside using MyProperty.
Another way of declaring a property
You can also declare properties in the following way.
public MyClass MyProperty { get;set; }
public string CategoryName { get;set; }
Here, you can see that we have declared properties without declaring private data member for them. This works in the same way as the first code snippet as internally, .NET declares a private member for this property.
What is the difference between Shared and Static?
They both mean the same. Shared is used in VB.NET. Static is used in C#.
When the static keyword is used to declare a class, the member in context must be directly invoked from the class, rather than from the instance. Consider the following example
//Consider writing the following line of code...
Console obj = new Console();
obj.Writeline("Vishal likes static members"); //This line does'nt print
//This does'nt work, because WriteLine is a static method defined in the class Console
//The Console class is a static class
To use static members, give a reference to the exact class, as an instance in this case won't work.
To make this work, write...
Console.Writeline("Vishal likes static members");
To work with members of static classes, no need to create their instances.
Static Member - A class member declared with the keyword static is a static member. A static member is owned by the class, not by its instances (objects of the class).
Note that static members are actually class members, while non-static members are instance members (means they are owned by the instances). Both in C# & VB.NET, we may create static/shared events, properties, fields and functions.
Note Indexers in C# cannot be declared static.
Note Static member functions cannot access non-static members directly.