C# TryGetValue (C-Sharp)

SomeObject objectName;

_someDictironary.TryGetValue(Key, out objectName);

This is how you can retrieve an object from a dictionary using a key. If the key doesnt exist in the dictionary, it won’t return anything, and if you are using the objectName object somewhere, you will get a not set to an instance exception. Here is a better way :

SomeObject objectName;

if(_someDictironary.TryGetValue(Key, out objectName)){
// do something to the object
}else{
//
}

C# uses of using keyword (C-Sharp)

Using keyword is like import keyword in java, but it s much more stronger.

using FiratAtagun.Common;

This statement will import Common libraries of FiratAtagun library. Common libraries will be available to use inside the class to be used.

using fac = FiratAtagun.Common;

This statement aliases fac to FiratAtagun.Common library.

Yet another way of using the using keyword is inside methods as follows :

using(var someObject = new SomeObject()){
// do something here.
}

Cool thing about this block of code is that you don’t have to explicitly wrap your code with try/catch/ finally blocks, they are implicitly being used within the environment.

C#-? and ?? operators (C-Sharp)

Some of the variables (eg: int) can not be null by default. C# introduces ? operator so that developers can assign null value to the variables as following :

int foo = null // This wont compile.

int? foo = null // This will compile

?? operator is like an if statement which checks for null values of the variables as following :

int? foo = null
int num = 2010;

int bar = foo ?? num; // This will work and assign 2010 to bar. It s like an if statement that says, if the the value of a variable is null take the other one. Well, the other one can be null also right ?

But this is how you can assign a nullable type to a non-nullable type. If you tried this without using ?? operator it wouldnt compile.

You can do the same operation with ?: kinda if statement as follows :

int something= foo.HasValue  ? foo : 2010 ;

C# ref and out (C-Sharp)

C# allows passing variables by reference and value.

Passing a variable using ref the variable should be initialized.

Passing a variable using out the variable doesn’t need to be initialized.

int i = 1;

foo(ref i);

public static void(ref int j){
j = 2;
}

Now the value of i is 2.

int i;

foo(out i);

public static void(out int j){
j = 2;
}

Now the value of i is also 2.