How to Get Type Name without full namespace in C# ?

If you want to find the full name of the type in C# , you can use the typeof keyword to do it as shown in the below code snippet.

 var str1 = typeof (Author).ToString();

The problem with this method is that it displays the full name along with the namespace.

If you want to get only the class name without namespace , you can use the Name property of MemberInfo class as shown below.

var str2 = typeof(Author).Name;
Console.WriteLine("using typeof(Author).Name results in " + str2);

Read more

Constant->static and readonly->instance field in C#

There are 2 options for the developers to declare a constant value in their C# application.
1. readonly
2. const

If you need to initialize a constant field at runtime , you must use readonly instead of const. const fields can only be initialized at compile time i.e they must be assigned a value at the time of declaration. You cannot modify it after its declaration.

The readonly fields can be either initialized at the time of declaration and/or within the constructor of the class. This means that you can initialize it at runtime (only within the constructor.

public class Student
{
    public readonly int age = 0;
    public const int marks = 0;

    public Student(int id, int age, int marks)
    {
        // You can assign the value for readonly field in the constructor
        this.age = age;

        // You cannot assign the value in the constructor for const field
        //this.marks = marks;
    }
}

Read more

3 options to return multiple values from a method in C#

There are many occasions when you might want to return multiple values for a method in C#. Below are some of the options that the developers could use to return multiple values from a method in C#.

Different Options to return multiple values from a method in C#

Option 1 : Using out keyword on parameters that will behave like return parameters.

The following method accepts id as input and returns firstname and lastname using the out keyword.

static void ReturnEmployeeName(int id, out string firstName, out string lastName)
{
    // Get the employee data by id
    firstName = "Senthil";
    lastName = "Kumar";
}

Read more

BUILD 2016 Live Streaming

iframe#videoplayeriframe { background-color: white; /* remove or change this declaration to look nice on the parent site */ border: 0px solid white; height: 1000px; /* this is a guess of the default height of the embedded page */ overflow: hidden; padding: 0px; width: 780px; /* change this declaration to look nice on the parent site */ }

Read more

Windows 10 Step by Step Tutorial – Copy and Paste Functionality

Introduction

There are various ways in which you can exchange data between apps in Windows Apps. Some of the techniques include
  • Copy and Paste
  • Share Contract
  • Drag and Drop
Most of the default controls that are available in XAML supports the basic clipboard operations but there are times when you might want to implement the copy and paste programmatically. This tutorial introduces you to the new functionality in Windows Universal App (UWP) which lets the developers to integrate the clipboard operations in their apps.

Tutorial – How to integrate Copy and Paste functionality in UWP apps?

Integrating the Copy and paste functionality in a UWP app is a four step process 1. Add the namespace “Windows.ApplicationModel.DataTransfer” to the code behind file. Create an instance of the DataPackage class and specify the RequestedOperation property to the desired functionality.
DataPackage dataPackageobj = new DataPackage 
{ 

   RequestedOperation = DataPackageOperation.Copy 

}; 

Read more

How to run Visual Studio 2015 in safe mode?

There are times when you Visual Studio instance might not start up correctly. One of the best option during this time is to use Microsoft Visual Studio in safe mode. By running the Visual Studio in safe mode, you work with the default environment. In this mode, all the third party extensions would be disabled. This would help you identify if at all the problem was caused by any third party add-ins.

How to run Visual Studio 2015 in safe mode?

To run Visual Studio 2015 in safe mode, open Developer Command Prompt for VS2015 and type the command devenv.exe /safemode

After the command is executed, Microsoft Visual Studio 2015 will start in safe mode which is indicated in the title bar of Visual Studio 2015.

Read more