C#.NET Const

C#.NET const keyword is used to create a variable which can be initialized with a value but cannot be assigned any value after creating it.

C#.NET program with const keyword

Program

using System;

namespace ConstKeyword
{
    class Program
    {
        static void Main(string[] args)
        {
            //you can intialize
            const int i = 1;
            //you cannot assign value
            //i = 12; //error in this line
        }
    }

}
Saturday, 9 January 2016
Posted by Sudhir Chekuri
Tag :

Creating custom dll, exe and consuming dll

About dll

  • dll stands for "Dynamic Link Library".
  • It is an assembly.
  • It is a supportable file.
  • It is also known as "deployed namespace", which is a collection of classes.
  • Namespaces are used in .exe files by adding them as references.
  • Namespaces are imported using the keyword "using".

Creating dll

  • Open File click on "New project".
  • Under visual C# select "Windows",in that select "class Library" ,give a name as "techminds"and click on ok.
  • Then,the namespace is created with the name "techminds".
  • Write code in it ie., a static class with a public method.
  • Next build "build solution" in menu or press F6 to create a dll file.
  • Then check for the location where it is stored.
  • Open your project in file explorer. dll file will be create in the bin/debug folder in your project folder.

Steps to create exe file

  • Open File from to click on"New project".
  • Under visual C# select "windows", In that select "Console application", give name as "techmindsexe" and click on ok.
  • Then, write the code and add reference to that by following the below steps.

Steps to add Reference

  • Right click on "references" folder in "solution explorer".
  • click on "add reference".
  • click on "browse".
  • Browse the .dll file and click on ok,then the dll name will be added in the reference list.
Now we can add it as a namespace using the keyword "using".
Posted by Sudhir Chekuri
Tag :

C#.NET Access Specifiers - Private Public Protected Internal Protected Internal

Access specifiers are used to define scope of a type as well as its members ie.,who can access it and who can't access it.
C#.NET supports five different access specifiers. They are:
1)Private
2)Public
3)Protected
4)Internal
5)Protected Internal

Default access specifier of method is private.
Default access specifier of class is internal.

Private

Members declared as private cannot be accessed outside the block.
Members declared as private within class or structure aren't possible outside of them in which they are defined.
In C# the default scope for members of a class or structure is "Private".

Public

Members declared as public can be accessed from anywhere.

Protected

Members declared as protected can be accessed only in the inherited classes

Internal

Members declare as internal can be used only with in the project.
Members which are declared as internal were accessible only with in the project both from child or non-child classes.
The default scope for class is Internal.

Protected Internal

Members declared as Protected Internal, enjoy dual scope ie., Internal and Protected.
Within the project they behave as internal, providing access to all other classes.
Outside the project they behave as Protected, and still provide access to child classes
Tuesday, 5 January 2016
Posted by Sudhir Chekuri
Tag :

C#.NET Abstraction

Hiding the complexity and providing the services is called abstraction
In other words hiding the unnecessary data and providing required facilities is called abstraction.
Example: System.Console.ReadKey( );
ReadKey( ) is the method under console class in System namespace which is used directly without bothering about the complex code written in it using abstraction.

Abstraction is achieved using access specifiers.
Posted by Sudhir Chekuri
Tag :

C#.NET Abstract Class

Abstract class is a class with both abstract methods and normal methods.
Abstract methods will have only definition and they will not have implementation.

A class that inherits the abstract class should implement the abstract methods in the abstract class by overriding it.

Example program for abstract class

Program

using System;
abstract class ShapesClass
{
    abstract public int Area();
}
class Square : ShapesClass
{
    int side = 0;

    public Square(int n)
    {
        side = n;
    }
    // Area method is required to avoid
    // a compile-time error.
    public override int Area()
    {
        return side * side;
    }

    static void Main()
    {
        Square sq = new Square(12);
        Console.WriteLine("Area of the square = {0}", sq.Area());
        Console.ReadKey();
    }

}

Output

Area of the square = 144
Posted by Sudhir Chekuri
Tag :

C#.NET Sealed Class

  • A class which cannot be inherited by any other class is known as Sealed class.
  • It should be declared using "sealed" keyword.
  • Sealed classes are used to restrict the inheritance feature of object oriented programming.
  • Once a base class is defined as Sealed class,this class cannot be inherited.
  • If a class is derived from a Sealed class, compiler throws an error.
Syntax for sealed class

sealed class classname

{

//methods

}

class classname2:classname//Invalid because derived class will not inherit sealed class.

Example for sealed class

Program

using System;

namespace SealedClass
{
    class Program
    {
        static void Main(string[] args)
        {

            SealedClass sealedCls = new SealedClass();
            int total = sealedCls.Add(4, 5);
            Console.WriteLine("Total = " + total.ToString());
            Console.ReadKey();
        }
    }
    // Sealed class
    sealed class SealedClass
    {
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
}

Output


Total = 9
Posted by Sudhir Chekuri
Tag :

C#.NET static class

C#.NET Static class should contain only static methods.

Static methods cannot be called using reference.
We cannot create object for Static class

Example program for C#.NET Static class
Program

using System;
namespace StaticClass
{
    class Program
    {
        static void Main(string[] args)
        {
            a.add();
        }
    }

    static class a
    {
        static public void add()
        {
            Console.WriteLine("add");
        }
    }

}
Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -