Wednesday, September 17, 2014

Static and Abstract keywords in C#

Access modifiers:

public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private
The type or member can only be accessed by code in the same class or struct.

protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

Static

The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created. A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
Making a class static just prevents people from trying to make an instance of it. If all your class has are static members it is a good practice to make the class itself static.



class SomeClass 
    {
    public int InstanceMethod() { return 1; }
    public static int StaticMethod() { return 2; }
    }

In order to call InstanceMethod, you need an instance of the class:

SomeClass instance = new SomeClass();
instance.InstanceMethod();   //Fine
instance.StaticMethod();     //Won't compile

SomeClass.InstanceMethod();  //Won't compile
SomeClass.StaticMethod();    //Fine



Abstract class

Abstract classes, marked by the keyword abstract in the class definition, are typically used to define a base class in the hierarchy. What's special about them, is that you can't create an instance of them - if you try, you will get a compile error. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.  

Let's see how it works



Since all the members in the class Bob(name, alias, age) are declared public, it can be accessed outside the class Bob.



Here only alias is declared public, both age and name are declared private. Hence we can access only alias outside the class Bob. 


Function in C# Console Application

A function allows you to encapsulate a piece of code and call it from other parts of your code. You may very soon run into a situation where you need to repeat a piece of code, from multiple places, and this is where functions come in. In C#, they are basically declared like this:
<visibility> <return type> <name>(<parameters>)
{
        <function code>
}

To call a function:


PrintFooToScreen();

Example 



Output

Friday, September 12, 2014

More Examples on Console Application

Example 2



In the above example a string is being stored in a variable ‘tommy’ so that we can reuse it wherever we want it in the program.





Example 3

In the following example we will print something to the user and we want the user to respond back.
ReadKey() – Reads a single key from the user. Basically blocks the program until the key is pressed. 




  •  The message ‘type a number , any number?’ printed on screen
  • The key entered by user is stored in keyInfo
  • The value in keyInfo is converted to string and taken in {0}










Wednesday, September 3, 2014

A Word About C# dotnet

C# is a programming language designed for building a wide range of enterprise applications that run on the .NET Framework. Visual C# .NET enables developers to build solutions for the broadest range of clients, including Web applications, Microsoft Windows Forms based applications, and smart devices. C# developers can leverage their existing C++ and Java skills and knowledge to be successful in the Microsoft .NET development environment.C# is the language and dotnet is the library that the language uses to manipulate things on the computer. Specific libraries contains the functionalities to complete the particular task.

Installation


Download and install Visual Studio Express 2013 for Windows Desktop from the link








Creating new Project


You have lots of options. But everything we are going to do here is on C# dotnet. We want to create Windows application. There are diff templates. All these templates are different project type. They are stored with foundations already built for you. You don’t have to start from the scratch everytime you create an application. Easiest type of application is the Console Application.


Console Application

Console Application is the text based application that runs from the command concept.




Example 1



  • ·        On the right hand side you will see Solution Explorer which contains all the files  of your project
  • ·     Program.cs in the Solution Explorer is the Source code file. It contains all the  source code in the program.
  • ·        AssemblyInfo.cs contains information about your program.
  • ·        The class ‘Program’ contains the function called ‘Main’.
  • ·        The WriteLine is one among the functions available in class Console.
  • ·        // says the compiler to ignore this, its only a comment. 
   
    Save your program


    Run your program


   
   Output