Tutorialspoint

C# diff b/n objects and classes

using System;

namespace BoxApplication {
   class Box {
      private double length;   // Length of a box
      private double breadth;  // Breadth of a box
      private double height;   // Height of a box
      
      public void setLength( double len ) {
         length = len;
      }
      public void setBreadth( double bre ) {
         breadth = bre;
      }
      public void setHeight( double hei ) {
         height = hei;
      }
      public double getVolume() {
         return length * breadth * height;
      }
   }
   class Boxtester {
      static void Main(string[] args) {
         // Creating two objects
         Box Box1 = new Box();   // Declare Box1 of type Box
         Box Box2 = new Box();
         double volume;
         
        // using objects to call the member functions
         Box1.setLength(6.0);
         Box1.setBreadth(7.0);
         Box1.setHeight(5.0);
         
         // box 2 specification
         Box2.setLength(12.0);
         Box2.setBreadth(13.0);
         Box2.setHeight(10.0);
         
         // volume of box 1
         volume = Box1.getVolume();
         Console.WriteLine("Volume of Box1 : {0}" ,volume);
         
         // volume of box 2
         volume = Box2.getVolume();
         Console.WriteLine("Volume of Box2 : {0}", volume);
         
         Console.ReadKey();
      }
   }
}

C# Postfix Operator

using System;
class Program {
   static void Main() {
	
        int a, b;
        a = 10;
        Console.WriteLine(a++);
        
        b = a;
        Console.WriteLine(a);
        Console.WriteLine(b);
   }
}

C# Increment and Decrement Operators

using System;

class Program {
   static void Main() {
      int a, b;
        
      a = 10;
      Console.WriteLine(++a);
      Console.WriteLine(a++);
        
      b = a;
      Console.WriteLine(a);
      Console.WriteLine(b);
   }
}

C# Prefix Operator

using System;
class Program {
   static void Main() {
	
      int a, b;
      a = 50;
      Console.WriteLine(++a);
        
      b = a;
      Console.WriteLine(a);
      Console.WriteLine(b);
   }
}

C# Object Class

using System;  
namespace MyApplication {  
   class Demo {  
      protected string name = "Website";  
      protected void Display(string str) {  
         Console.WriteLine("Tabs: " + str);  
      }  
   }  
   class Test : Demo {  
      static void Main(string[] args) {  
         Test t = new Test();  
         Console.WriteLine("Details: " + t.name);  
         t.Display("Product");  
         t.Display("Services");  
         t.Display("Tools");  
         t.Display("Plugins");  
      }  
   }      
}  

C# generate first 100 even Numbers

using System;
class Program {
   static void Main() {
      int val;
      for(val = 1; val <= 100; val++) {  
          
         if(val%2 == 0) { 
            
            Console.WriteLine(val);  
         
         }  
      }
   }
}

C# GetType() method

using System;

class Program {
   public static void Main() {
      object[] values = { (int) 100, (long) 17111}; 
      foreach (var value in values) {
          
         Type tp = value.GetType();
         
         if (tp.Equals(typeof(int)))   
            Console.WriteLine("{0} is an integer data type.", value);
    
         else
            Console.WriteLine("'{0}' is not an int data type.", value);
      }
   }
}

C# Typeof() method

using System;
class Program {
   static void Main() {
      Console.WriteLine(typeof(int));
      Console.WriteLine(typeof(byte));
   }
}

C# Trim

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {

        string str = "   Amit  ";
        Console.WriteLine(str);
        
        // trim
        Console.WriteLine("After removing leading and trailing whitespace...");
        string res = str.Trim();
        Console.WriteLine(res);
        
        Console.ReadKey();
      }
   }
}

C# Random Numbers

using System;
class Program {
   static void Main() {
      Random rd = new Random();
        
      int rand_num = rd.Next(100,200);
        
      Console.WriteLine(rand_num);
   }
}

Advertisements
Loading...