Building your C# application

I am going to describe how to create a basic C# console application in which we generate a random number and then guess it.Before diving in I am just giving an introduction on C# and .NET and also which tools we will be using in building this application.

C# is an object oriented programming language created by Microsoft and it is one of the many languages used along with .NET framework. There is generally a confusion between C# and .NET, I will try to clear this. C# is basically a programming language and we use it along with .NET framework to build applications for Windows and we can also use C# to build applications for other platforms like Linux  by making use of Mono, which is a open source implementation of Microsoft .NET Framework.
.NET is a framework for building windows applications and web applications and this is not limited to one language. We can use C#, VB.Net, F#, C++, J# and many more like around sixty languages. It has two important components say Common Language Run-time and Class Library. CLR is a virtual machine component and manages execution of .NET programs. Common Language Runtime or CLR is the run-time execution environment of .Net Framework. Converting MS-IL into platform or OS specific code is done by the CLR. CLR is responsible for bringing application to life and its also the CLR’s job to tear down application when its finished executing or if it has an unrecoverable error. CLR actively tracks all the memory a program uses and it knows when the program is finished with memory so it will clean things up and allows program to have enough memory as it runs. 

We make use of an IDE(Integrated Development Environment) to build our application. We use Visual Studio (Community edition) as it is free.

First we should decide the algorithm on how we are writing our program. The algorithm is as follows:

1.Generate a random number .
2.Take input from user.
3.Check whether both are equal.
4.Make user know whether value he entered is greater or lesser than the secret number.
5.Continue steps 2 ,3,4 until guess is correct.
6.If user quits close or else continue from step 1 again.

Get Microsoft Visual Studio installed and Create a new project. In creating new project select Console Application and select location where you want

to save the project.

Once you click on OK you have a new screen with defaults loaded. We have a program.cs file created for us with an empty main method which is our entry point.


Now we can start writing our application. First we need to generate a random number for this we make use of Random class .


After generating this number we should prompt the user to guess a value.


Now we should check whether the guess and secret number are equal or not. We should do this until user enters the correct value.


We should allow user to play the game until he wants to quit.


now the application is done and we can start using it to play with your friends.

Nancy

Nancy is a light-weight ,low ceremony framework for building HTTP-based services on .NET. It provides a super-duper-happy-path to all interactions.

You might be wondering what the super-duper-path is. Here I will explain things clearly.

 

  • It just works
  • Easily Customizable
  • Low ceremony
  • Low friction

how to pass a javascript object to be written to a database

I have an Address object from my user interface like below

Address:{address1:”Franklin”,address2:”Warrensburg”,adress3:”Missouri}

so as i am sending an object i should be able to capture all the fields.

so i define a class as

public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
}

and now when we get an object ,we get values as follows

[“Address”] = new JObject
{
[“Address1”] = _customer.Address.Address1, ===>Franklin
[“Address2”]= _customer.Address.Address2, ===>Warrensburg
[“Address3”] = _customer.Address.Address3 ===> Missouri
}

Object Class

Object class supports all classes in .Net Framework class hierarchy and provides low level services to derived classes. This is the base class of all classes in the .NET framework .

Handling “Cannot read configuration file due to insufficient permissions” error while hosting a website using IIS

When you are hosting a website using IIS you may get an error like this.

This is because of not providing required permissions to the folder which contains our website and its supporting documents. This error is resolved by modifying permissions given to the folder.
Right click the folder and go to properties.
Go to Security tab and add user “Everyone” and give all permissions.
Now refresh the page in browser. Your problem is solved.

Explain about JIT

In .Net framework we have just in time compiler present in Common Language Runtime which converts MSIL code into native code.

Different types of  Jit are:

  • Pre-Jit -Compiles complete source code into native code at the time of deployment
  • Ecno-Jit -Compiles the code associated with required item like loading form or button click etc
  • Normal Jit -Compiles the code required and stores in cache, whenever the same thing is required it loads from cache.
By default we have normal Jit in CLR. To use pre jit we make use ngen.exe. In theory it seems that Pre-Jit is useful but when you look at realty Jit compiles and generates native code specific to machine depending on the various properties of the run time environment ,so it is beneficial to go with normal jit.

what is MSIL

MSIL is Microsoft intermediate language. When we compile a program in .net, the source code is converted into an intermediate language called Microsoft Intermediate Language. At this point the code is partially compiled and it cannot be run on the operating system. When we run the application this partially compiled code is compiled to machine specific  by the Just In Time compiler present in Common Language Runtime using the operating system  environment properties like CPU,OS etc

difference between “IS” and “AS”

“IS” keyword can be used to check whether two variables are of same type.

Ex:
object obj=”narendra”;
object obj1=1234;

if(obj is string)
{
Console.WriteLine(“Success”);//this statement executes as obj is string
}

if(obj1 is string){
Console.WriteLine(“Success”);//this statement is not executed as obj1 is not string
}

“AS” keyword is used to convert variables from one type to another and if conversion is not successful then null is stored.

string str=obj as string;//here it succeeds and str has value of “narendra”

string str1=obj1 as string;// here it fails and str1 has null value

Explain about WPF?

WPF(Windows Presentation Foundation) is a graphical subsystem to display user interfaces. One should be wondering we have win forms for this then what is need for WPF. WPF has the following advantages over win forms.

Anywhere execution

We use XAML in wpf which makes it possible to use the same code for various types of applications like windows, web or silver light etc.

Binding

Binding objects is very easy in WPF. Without writing single code we can bind two objects.

Common Look and Feel

We can make use of styles and the same style is applied to our entire application as and when required.

Declarative Programming(XAML)

We describe abstractly what need to be done and visual studio will do the directive programming for us in the back end.

Express Blend and animation

WPF internally uses Directx, directx is used for animation, so WPF can do animation as well. Express Blend is a tool which is used to create animation and reuse it.

Fast Execution

WPF uses directx and supports software, partial, hardware rendering depending on requirement, whereas win forms uses software rendering only.

Graphic Independent

WPF uses DIP(device independent pixel) 1 dip =1/96 of an inch and it adjusts itself to any screen based on this .