Translate

Thursday, 25 May 2017

C# Namespaces Tutorial

Topic : Namespaces in C#
Q 1.) First question will come in mind is what are namespaces and why it came in existence?

Before answering this question let me ask you something : 

Q 2.) Can you declare two classes with same name in one file?
You will say NO.
See the code and compile it: 
using System;
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
}
class MainClass{
}

Output(After compilation you will get  error as mentioned below) : 
prog.cs(7,7): error CS0101: The namespace `global::' already contains a definition for `MainClass'
prog.cs(2,7): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings

Q 2.) How can we declare two classes with same name in one file only?
Ans) So here come the concept of namespaces, Yes we can declare with the help of namespaces 
 See the below code and run it in your system. You will get to know that we can declare two classes with same name in one file with the help of namespaces:
using System;
namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
}
}
namespace again_main_class{
        class MainClass{
        }
    }

Output: Hello World




Q 4.) What are Namespaces in C#?
Ans. ) As we discussed above There are some definitions of namespaces like:
1. It is collection of classes. 
As you can see in both above examples that it can contain as many classes it want.
2. It is used to differentiate one set of name from another set of name. 
See the below code for explanation: 

using System;
namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}
namespace again_main_class{
        class MainClass{
            void fun(){
                Console.Write("this is again_main_class namespace fun");
                }
        }
    
    }

So in the above code you can see that we are having fun() two times but in different namespace .so by this way we can say that it is used to differentiate between one set of name from another set of name.



So I think that you got what are namespaces and why we used them.

Now Let me start how to use namespaces and member inside namespace 

Let me explain with the help of one code: 

using System;
namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}

Explanation: 
1. namespace main_class{
}

This is known as namespace declaration where.
namespace : keyword
main_class: name of namespace

2. namespace contain one class MainClass and MainClass contain two methods fun() and main(). So I think there is nothing to explain in that.
3. What is this line using System
So here System is the namespace name, which contain Console class.
So in this example we are using Console.WritelLine() thats why we have to include System namespace.

Now Let me tell you one thing: 
What if I will not include this System namespace then how can I use this Console class?

Yes I can use this class without writing using System as below:

System.Console.WriteLine();


Let me explain with the help of one more code that how to access member of namespaces: 

using System;

namespace first_namespace{
     class First{
        public void fun(){
            Console.WriteLine("hi this is inside first_namespace");
        }
    }
}

namespace second_namespace{
     class Second{
        public void fun(){
            Console.WriteLine("hi this is inside second_namespace");
        }
    }
}

namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
        
        first_namespace.First obj=new first_namespace.First();
        obj.fun();
        second_namespace.Second second=new second_namespace.Second();
        second.fun();
        
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}



Explanation: 
1.As you can see we  have fun() in both the namespaces, so to access fun() we have to use namespace name at the time of object creation.

Means:
Namespace_name.class_name object_name= new Namespace_name.class_name();

3. To avoid this namespace_name.something we can include that namespace name at the top just like using System
Example: In above example we can write our code as follows also:

using System;
using first_namespace;
using second_namespace;

namespace first_namespace{
     class First{
        public void fun(){
            Console.WriteLine("hi this is inside first_namespace");
        }
    }
}

namespace second_namespace{
     class Second{
        public void fun(){
            Console.WriteLine("hi this is inside second_namespace");
        }
    }
}

namespace main_class{
class MainClass{
    static void Main(string[] args){
        Console.WriteLine("Hello World");
        
        First obj=new First();
        obj.fun();
        Second second=new Second();
        second.fun();
        
    }
    
    void fun(){
        Console.Write("this is main_class namespace fun");
    }
}
}




So Thanks guys I hope now you have idea about namespaces in C#. if any doubt is there or you want to share some information about namespaces then please share in the comment.

Monday, 8 May 2017

How can I update youtube-dl?

Try

sudo pip install --upgrade youtube-dl
That will upgrade youtube-dl. If you get this message:

sudo: pip: command not found
try

sudo apt-get install python-setuptools
sudo easy_install pip
sudo pip install --upgrade youtube-dl

To install the latest youtube-dl in Ubuntu, run the following commands in a terminal:

 
Step 1: udo add-apt repositoryppa:nilarimogard/webupd8
Step 2: sudo apt-get update
Step 3: sudo apt-get install youtube-dl
To download a YouTube video, use the following command:

youtube-dl YOUTUBE-URL

Monday, 7 November 2016

Features of C#(C sharp)

There are some of the important features of C#
- Object Oriented
- Simpler Language
- Robust
- Architecture Neutral/portable
- Secure
- Support for multithreading at language level
- Designed to handle distributed applications

Wednesday, 22 June 2016

Java Switch Construct

Hi friends ........

                          selection construct

There are two types of constructs in Java...
        1.if-else
        2.switch-case

Note: if-else you know very well in c/c++ language.In java also if-else have same concepts.

                                              switch-case

There are some points about switch........
1.switch can only have Integer or Integer expression,string,enum.
2.case should always have a constant value or literal.
3.if you not used break inside case then it also executes all the case(followed the case which matches)until the break found or end of switch reached.

Demo:

public class Switch{
 public static void main(String args[]){
  //switch
  int x=8;


  switch(x){
   case 2:
      System.out.println("yo its x=2");
      break;
   case 4:
      System.out.println("yo its x=4");
      break;
   case 6:
      System.out.println("yo its x=6");
      break;
   case 8:
      System.out.println("yo its x=8");
      break;
   default:
      System.out.println("yo its >8");

  }
 }
}

output:

yo its x=8

Java Ternary Operator

                            Ternary Operators
Def: In ternary operators we use 3 operands.

syntax:
        expr1 ? expr2 : expr3

Note: Result of expr1 should be boolean in java.

Demo:

public class Ternary{
 public static void main(String args[]){
  //ternary operator

  int a=5;

  System.out.println( a>4 ? "a is greater than 4" : "a is less than equal to 4");
 }
}

output is:
a is greater than 4

Tuesday, 21 June 2016

Java Binary Operators Tutorial

                                      Binary Operators

Def: Those operatos which are applied on two(2) operands known as binary operatos.

Type of binary operators:
                        1.Arithmetic operators
                        2.Relational operators
                        3.logical operators
                        4.Shift and bitwise operatos
                        5.Assignment operators

I will discuss about each of the above types with examples.If still you have doubt then comment below..
                          Arithmetic Operators

We all are very familiar with these operators.
Arithmetic operators are further classified in 2 type:
1.Multiplicative   like : *,/,%
2.Additive         like : +,-

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_arithmetic{
 public static void main(String args[]){
  int n1,n2;
  n1=10;
  n2=5;

  int sum,diff;
  sum=n1+n2;
  diff=n1-n2;

  //Additive operators
  System.out.println("n1 + n2 is :"+  sum);
  System.out.println("n1 - n2 is :"+  diff);

  //multiplicative operators
  System.out.println("n1 * n2 is :"+  n1*n2);
  System.out.println("n1 / n2 is :"+  n1/n2);
  System.out.println("n1 % n2 is :"+  n1%n2);


 }
}


output of above code is:
n1 + n2 is :15
n1 - n2 is :5
n1 * n2 is :50
n1 / n2 is :2
n1 % n2 is :0
                                       Relational Operators

Relational operators are also generally classified in two category:
1.Relational operators     like: <, >, <=, >=, instance of
2.Equality operators       like: ==, !=

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_relational{
 public static void main(String args[]){
  
  //Equality operators
  int a,b;
  a=10;
  b=9;

  if(a==b){
   System.out.println("a is equal to b");
  }
  
  if(a!=b){
   System.out.println("a is not equal to b");
  }


  //Relational operators

   if(a<b){
    System.out.println("a is less than b");
   }

   if(a>b){
    System.out.println("a is greater than b");
   }

   if(a<=b){
    System.out.println("a is less than equal to b");
   }

   if(a>=b){
    System.out.println("a is greater than equal to b");
   }


   //instance of 
   //this is used for finding that a object is belong to a class or not

   Binary_relational obj=new Binary_relational();

   if(obj instanceof Binary_relational){
    System.out.println("obj is instance of class Binary_relational");
   }


 }
}

output of above code is:
a is not equal to b
a is greater than b
a is greater than equal to b
obj is instance of class Binary_relational
                    Logical Operators

There are two types of relational operators :
And &&
Or ||

Just read the demo for better understanding  gud luck.
DEMO :


public class Binary_logical{
 public static void main(String args[]){
  //And &&  Or ||

  int a=9;
  int b=8;
  int c=10;

  if(a>b && a>c){
   System.out.println("a is largest among three");
  }

  if(a>b || a >c){
   System.out.println("a is greater than either from a or b or both");
  }
 }
}

output of above code is:

a is greater than either from a or b or both
                                 Shift Operators and Bitwise operators

Shift operators are : <<, >>, >>>
bitwise operators are : bitwise And &,bitwise Or |, bitwise ex-or ^

Just read the demo for better understanding  gud luck.
DEMO :

public class Binary_shift_bitwise{
 public static void main(String args[]){

  //shift operators
  int x=10;

  int left_shift=x<<1;
  System.out.println("left shift of x by 1 bit is :" + left_shift);

  int right_shift = x>>1;
  System.out.println("right shift of x by 1 bit is :" + right_shift);

  //>>>(zero filled right shift)
  int zero_filled_right_shift= x >>>1;
  System.out.println("zero_filled_right_shift of x by 1 bit is :" + zero_filled_right_shift);
 
     //bitwise operators
     /*  & (bitwise and)
            Binary AND Operator copies a bit to the result if it exists in both operands.

            Example: (A & B) will give 12 which is 0000 1100
        */

        int a,b;
        a=60;
        b=13;
           int and_result=a&b;
           System.out.println(" a & b is:" +  and_result);

        /* | (bitwise or)
           Binary OR Operator copies a bit if it exists in either operand.

           Example: (A | B) will give 61 which is 0011 1101
        */
            int or_result=a|b;
            System.out.println(" a | b is:" +  or_result);

           /*^ (bitwise XOR)
              Binary XOR Operator copies the bit if it is set in one operand but not both.

              Example: (A ^ B) will give 49 which is 0011 0001

           */
            int xor_result=a^b;
            System.out.println(" a ^ b is:" +  xor_result);

 }
}

output is:

left shift of x by 1 bit is :20
right shift of x by 1 bit is :5
zero_filled_right_shift of x by 1 bit is :5
 a & b is:12
 a | b is:61
 a ^ b is:49
                         Assignment Operators

there are many variations of this like ; =,+=,-=,*=,/=,%=,>>=,<<=,>>>=

a+=b  is same as a=a+b
a-=b is same as a=a-b
and so on

I think there is no need for demo in this if u still have doubt then comment below i will show u demo...

JAVA Operators Tutorials

Hello friends.....
Today I will write about Operatos in Java in the best way so that you can understand this very well.

Q.what are the operators?
Ans.Operators are special symbols, Which applied on single or multiple operands and return some result.

Q.what are the types of Operators in Java?
Ans.there are 3 types of Operators in java.
    1.Unary      
    2.Binary       link:http://spojdev.blogspot.in/2016/06/java-binary-operators-tutorial.html
    3.Ternary      link:http://spojdev.blogspot.in/2016/06/java-ternary-operator.html
                                              
                                            Unary Operators

Definition:operators which are applied on single operand.

Types:
      1.Postfix expr       example: expr++,expr--
      2.Prefix expr        example: ++expr,--expr
      3.logical not        example: !(expr)
      4.bitwise not        example: ~
      5.sign indicator     example: +expr, -expr

Demo:

public class Unary{
 public static void main(String args[]){
 int x=5;

 //postfix expr
 System.out.println("x++ is:"+ x++);
 System.out.println("x-- is:"+ x--);
 System.out.println("x is:"+ x);



 //prefix expr
 System.out.println("++x is:"+ ++x);
 System.out.println("--x is:"+ --x);
 System.out.println("x is:"+ x);

 //logical not
 if(!(x==5)){
  System.out.println("x is not equal to 5");
 }
 else{
  System.out.println("x is equal to 5");
 }


 //bitwise not 
 System.out.println("bitwise not of aa is :" + ~x);



 //sign indicator
 System.out.println("its very simple like " +  -x);
 System.out.println("its very simple like " +  +x);



 }
}



ouput of demo is:

x++ is:5
x-- is:6
x is:5
++x is:6
--x is:5
x is:5
x is equal to 5
bitwise not of aa is :-6
its very simple like -5
its very simple like 5

Working With Java Collections