Showing posts with label C sharp. Show all posts
Showing posts with label C sharp. Show all posts

Monday, 5 February 2018

tyoeof()  
typeof is used to get the type based on a class. It takes class as parameter. If you pass object as parameter it throws an error.
object s = "Hello";

Type t = typeof(object);

Console.WriteLine(t);

Output: System.object


GetType()

GetType is used to get the type based on an object (an instance of a class).It takes object as parameter.

object s = "Hello";

Type t = typeof(object);

Console.WriteLine(t);

Output: System.object

Sunday, 4 February 2018

The following are the difference between abstract class and interface :
1)
A sub class can inherit/extends from only one abstract class i.e. multiple class inheritance is not supported in c# and java
eg:
public abstract class Animal
{
}
public abstract class Vertebrate
{
}
public class Dog : Animal,Vertebrate
{
//invalid
}
A Sub class can implement any number of interfaces i.e. multiple interface inheritance is supported in c# and java
eg:
public interface IAnimal
{
}
public interface IVertebrate
{

}
public class Dog : IAnimal,IVertebrate
{
//valid
}
2)
An abstract class can have both abstract and non abstract(concrete methods)
eg:
public abstract class Animal
{
public abstract void Breath();//valid
public void HasBackbone()//valid
{

}
}
An Interface should have only abstract  methods
eg:
public interface IAnimal
{
void Breath();//valid
void HasBackbone()//invalid
{
}
}
3)
An abstract class contains fields

eg:
public abstract class Animal
{
int i;//ok
}
An interface does not contain fields.
eg:
public interface IAnimal
{
int i;//invalid
}
4)
An abstract class can have constructor

eg:
public abstract class Animal
{
public Animal()
{
//Ok
}
}
An interface can't have constructor
eg :
public interface IAnimal
{
public IAnimal()
{
//Invalid
}
}
5)
Members of abstract can have access modifiers and default access modifiers of data member and member function/method inside an abstract class by default private and class itself has internal access modifier

eg :
abstract class Animal
{
public abstract void CanBreath();//Ok
}
Member function/method of interface cannot have access modifiers and by default all member function/method inside interface are public and interface itself has public access modifier
eg:
interface IAnimal
{
public void CanBreath();//invalid
}
6)
A sub class should not override/implement all methods inside abstract class and only override/implements abstract method

eg:
abstract class Animal
{
public abstract void CanBreath();
public  virtual void HasBackbone()
{

}
}

class Dog : Animal
{
public override void CanBreath()
{

}
}
A sub class should implement all the methods inside interfaces.
eg:
interface IAnimal
{
void CanBreath();
void HasBackbone();

}

class Dog : IAnimal
{
public void CanBreath()
{
}
public void HasBackbone()
{
}
}

Saturday, 3 February 2018

Using C# program Draw Result

 1         2

 2         3

 3         4

 4         5

Solution :

public static void SampleMethod()
        {

            for (int i = 1; i <= 4; i++)
            {
                for (int j = i; j <= i + 1; j++)
                {

                    Console.Write(j + " ");
                }
                Console.WriteLine();
            }
        }
Use the do-while loops in nested form and produces the following output in C#
 Multiple Tables

  1     2     3

  2     4     6

  3     6     9

Solution :

First Way

public static void SampleMethod()

        {

            int i = 1;

            int k = 1;

            do

            {

                int j = 1;

                do

                {

                    Console.Write(j * k + " ");

                    j++;



                } while (j <= 3);

                Console.WriteLine();

                k++;

                i++;



            } while (i <= 3);

        }
Second Way :
public static void SampleMethod()
        {
            int k = 1;
            for (int i = 1; i <= 3; i++)
            {

                for (int j = 1; j <= 3; j++)
                {

                    Console.Write(j * k + " ");
                }
                Console.WriteLine();
                k++;
            }
        }

Wednesday, 24 January 2018


C# (pronounced as "C-sharp") is a modern high-level object oriented and type safe programming language developed under the leadership of Anders Hejlsberg at Microsoft in 2002 for developing managed software applications. It is one of the most popular programming languages of .Net framework.
Platform
A platform could be hardware architecture and some software, on the top of which others applications operate. In another word, it is environment with required resources.eg. CLR (common language runtime) on which managed codes runs, JVM (Java virtual machine).
Framework
Framework is ready to use collections of classes and interfaces used for developing particular types of applications. Frameworks bring consistency in software development.eg.Net framework, spring and struts in java, Laravel and zend framework in Php.
Type safety


When data types of one type is assigned to other type unknowingly, we get the error at compile time or undesirable result at runtime and preventing such types of error is known as type safety. C# is a type safe language.
When we add integer and string value and assigned its result into integer variable throws  "cannot implicitly convert string to int" compile time error.
                       
There are two types of languages one is type safe and another is not. JavaScript is not a type safe language.
    <script>
        var number = 10;//integer
        var string = "10";//string
        var sum = string + number;//concate or addition ?
        alert(sum);//result 1010
    </script>



Categories

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page