RSS
 

Archive for the ‘C#’ Category

C# 3.0 New Features

28 Jul

In this post, i will write about all the new features in C# 3.0, this features will help you more and more in your programming life, and class design.

this list of C# features which will be covered here,

1. Extension method

2. Lambda expression

1. Extension Method

  • extension method is new feature in C# 3.0, it is like static methods which we know in our object oriented background but with more main differences:
    • Extension methods must written in static class not like static method which can written in any class
    • To specify extension method, you need to write this keyword before the first argument in the method, static methods don’t have this keyword at declaration
    • at consumed, the argument with this keyword isn’t passed to the function
    • extension methods can be called only on instances, calling them on class will result in compilation errors, the class instances on which they are called are determined by the first argument in the declaration in other words the argument has this keyword
    • Note that extension methods can’t access private methods of extended type
    • Note that if you don’t have source code of type, and you don’t add new methods to type
    • Note that if you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never called.
    • extension methods can’t be used to override existing methods
    • concept of extension methods can’t be applied to fields, properties or events
    • if the class is sealed than there in no concept of extending its functionality, for this a new concept is introduced

But why you determine that you want to implement this new feature, let’s assume the following scenario you need to implement new method in Int class to get the power of the integer, as the traditional thinking you will design class which inheriting from the parent class Int and implement new method called Power but with C# 3.0 you have another option implement extension method  Power() on Int class, let’s try it with your hand

 
No Comments

Posted in C#

 

DataGridView Empty Row

24 May

in datagrid view, you have one empty row and have an * on the lest most portion of the row. to solve this problem click on the DataGridView and you should see a little arrow in the top right corner. click on that once and you should see a number of options. some of those options have checkboxs and one of them should say “Enable Adding”. Make sure that it is not checked to solve this problem

 
No Comments

Posted in C#

 

Casting @ C#

23 May

using as keyword in c# in conversion from type to another type (CASTING) , use it like “expression as type”  type and expression are references type but what different when as fail to cast the expression to certain type it won’t through any expression but it will fill the expression with null



 
No Comments

Posted in C#

 

Assembly Qualified Name…Get It

30 Apr

before we start to know how we can get the qualified name of the assembly, let’s define first assembly ”Assemblies are the building blocks of .NET Framework applications; they form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality“ and assembly content

Assembly Content

then let’s define Global Assembly Cache :The global assembly cache stores assemblies specifically designated to be shared by several applications on the computer. Now, we can write code using reflection to can get the Fully Qualified Name of assembly”

public static void Main(){
        Type t = typeof(System.Data.DataSet);
        string s = t.Assembly.FullName.ToString();
        Console.WriteLine("The fully qualified name {0}.", s); }
My Source: http://msdn.microsoft.com/en-us/default.aspx
 
 

Show() & ShowDialog() @.Net

11 Apr

when you create new form windows you want to show it so you use Show() or ShowDialog(), so what is the difference? ShowDialog is the same as Show, it just shows the form as a modal window. Modal meaning the form cannot lose focus until it is closed. (The user can’t click on other windows within the same application.)

in other meaning, ShowDialog the form is shown in a modal window. A modal form generally is used to show a dialog that must be closed until it takes back to the caller…for instance, the messagebox.show shows the message as a dialog, so the user should close it before the code continues to the next line. In the other hand, the show method just shows the form and continues executing the next line of the caller, when you open a form from a menu you usually shows it as no modal.

 
No Comments

Posted in C#

 

Moment @ Nullable in .Net

23 Mar

Nullable value…why are we introducing …

in studying of object oriented concepts, we know that we have two data type Value type & Reference type the famous difference between the two is the.

  • value type contain the actual value of variable but,
  • reference type contain the address (reference) of the object

but there is another difference which may be some people don’t know it

  • value type can’t contain null value, if it hasn’t value, it will be default value (for example, int data type if it doesn’t have any value it will be Zero).
  • reference type allow null value.

now you get the difference, let ask you a question, Do we need null in value type ? …thinking… the answer is yes, we need it. let’s imagine that we have database contain employee which consist some columns may be allow null like Salary and so on, how you can map this @code, if you use default value … so you didn’t get the actual meaning of your business @your code. null value doesn’t means zero or empty string, it means that you don’t have enough information  . so there are difference … the solution is Nullable Value. Nullable value is new generic type which allow nullability of value type, Nullable<T>.

Nullable @C#

  • ? is new modifier means Nullable so when you need to define Nullable value type  int? i;
  • ?? operator can convert from Nullable type to non-Nullable type with default value. So “ int x;     int? y;     x = y ?? 0“ will assign the value of y to x if it is not null, and will assign the value 0 to x otherwise.
  • if you try to add two Nullable values and one of two is null value so the result of final addition will be NULL
  • For comparison operators, comparing two nullable types is always going to result in a bool value, not a bool? value.
int? nullableInt;
int? nullValue = null;
int? notNull = 123;
bool? answer1 = true;
bool? answer2 = false;
bool? answer3 = null;
int? nullableInt;
int? copy = nullableInt;    // Invalid as nullableInt is not yet assigned

int standardInteger = 123;
int? nullableInteger;
decimal standardDecimal = 12.34M;
// Implicit conversion from int to int?
nullableInteger = standardInteger;
// Explicit conversion from int? to int
standardInteger = (int)nullableInteger;
// Explicit cast from decimal to int?
nullableInteger = (int?)standardDecimal;
int? a = 55;
int? n = null;
int? result;
result = a * 2;         // result = 110
result = a * n;         // result = null

bool? result;
result = true & null;       // result = null
result = false & null;      // result = false
result = true | null;       // result = true
result = false | null;      // result = null
result = true ^ null;       // result = null
result = false ^ null;      // result = null

My Source: http://www.blackwasp.co.uk/CSharpNullableDataTypes.aspx

 
No Comments

Posted in C#

 
 
 

You need to log in to vote

The blog owner requires users to be logged in to be able to vote for this post.

Alternatively, if you do not have an account yet you can create one here.

Powered by Vote It Up