Alternative string concatenation of a two string in C#

public static string GetString(string string1,string string2)
{
char[] str1 = string1.ToCharArray();
char[] str2 = string2.ToCharArray();

string finalString = “”;
int length = str1.Length > str2.Length ? str2.Length : str1.Length;
for (int i = 0; i < length ; i++)
{
finalString = finalString + str1[i] + str2[i];
}
if (str1.Length > str2.Length)
{
int difference = str1.Length – str2.Length;
for (int i = 0; i < difference; i++)
{
char rem = str1[str2.Length + i];
finalString = finalString + rem;
}

}
else
{
int difference = str2.Length – str1.Length;
for (int i = 0; i < difference; i++)
{
char rem = str2[str1.Length + i];
finalString = finalString + rem;
}
}
return finalString;
}

string finalString = GetString(“USA”, “INDIA”);

Ouput : UISNADIA

Combinable enums in .net

As well as creating normal enums we can create combined enum values. To do this decorate the enum with a Flags attribute this allows the string to output the combination of names.We also need to provide specific values for each member and we do this power of two for example 0,1,2,4,8.

Example

[Flags]
public enum Sides
{
None=0,
Left=1,
Right=2,
Top=4,
Bottom=8
}
public class EnumCombinationsDemo
{
public static void Test()
{
var leftRightCombination = Sides.Left | Sides.Right;//Output : Left | Right
var topBottomCombination = Sides.Top | Sides.Bottom;//Output : Top | Bottom
bool isTopIncluded = topBottomCombination.HasFlag(Sides.Top); //Output : true
//Combining combinations
var allCombinations = leftRightCombination | topBottomCombination; //Output : Left | Right | Top | Bottom
//toggling values
allCombinations ^= Sides.Top; //Output : Left | Right | Bottom
allCombinations ^= Sides.Top; //Output : Left | Right | Top | Bottom
}
}

Enjoy coding…

Expose internal types and members to another assemblies

In Some scenarios we might want to access some of the internally protected item to outside the assembly. By using the attribute [assembly: InternalsVisibleTo(“AssemblyName”)] in the assemblyInfo class you can access
the all the members and internal types to another assemblies.
For example if i want to access the below internal class and method in to another assembly
namespace InternalThings
{
internal class InternalClass
{
internal void InternalMethod()
{
}
}
}

for example DemoInternal, Just a add an attribute to assemblyInfo class
[assembly: InternalsVisibleTo(“DemoInternal”)] in the InternalThings Assembly ie, Our Internal members existing project or assembly.

Enjoy Coding…

Runtime Conversions with Convert.ChangeType

Sometimes we may need to convert to one type to another at runtime but we don’t know the type in advance. In this situations we can use Convert.ChangeType() method.
In this below code i had converted the value 100 which is a string and this value converted to int and double at runtime
object initialValue = "100";
Type targetType = typeof (int);
object convertedValue = Convert.ChangeType(initialValue, targetType);
Type convertedType = convertedValue.GetType();
Console.WriteLine(convertedType);
targetType = typeof(double);
convertedValue = Convert.ChangeType(initialValue, targetType);
convertedType = convertedValue.GetType();
Console.WriteLine(convertedType);

Enjoy Coding…