Tutorialspoint

C# Stack class

using System;
using System.Collections;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Stack st = new Stack();
         
         st.Push('H');
         st.Push('I');
         st.Push('J');
         st.Push('K');
         st.Push('L');
         
         Console.WriteLine("Count: "+st.Count);
      }
   }
}

C# LINQ to sort a list

using System;
using System.Collections.Generic;
using System.Linq;

class Demo {
   static void Main() {
      List<string> myList = new List<string>();
      myList.Add("truck");
      myList.Add("bus");
      myList.Add("cab");
      myList.Add("motorbike");

      var myLen = from element in myList
         orderby element.Length
         select element;

      foreach (string str in myLen) {
         Console.WriteLine(str);
      }
   }
}

C# LINQ

using System;
using System.Linq;

class Demo {
   static void Main(){
      int[] arr = { 87, 92, 45, 65, 34, 88 };
        
      Console.WriteLine(arr.Average());
        
      Console.WriteLine(arr.Count());
   }
}

C# object data types

using System;
using System.IO;

namespace Demo {
   class objectClass {
      public int x = 200;
   }

   class MyApplication {
      static void Main() {
         object obj;
         obj = 50;
         Console.WriteLine(obj);
         Console.WriteLine(obj.GetType());
         Console.WriteLine(obj.ToString());
    
         obj = new objectClass();
         objectClass newRef;
         newRef = (objectClass)obj;
         Console.WriteLine(newRef.x);
      }
   }
}

C# objects

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# implicit type conversions

using System;
using System.IO;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         int val1 =11000;
         int val2 =35600;
         long sum;

         sum = val1 + val2;
 
         Console.WriteLine("Sum= " + sum);

         Console.ReadLine();
      }
   }
}

C# file operations

using System;
using System.IO;

namespace FileIOApplication {
   class Program {
      static void Main(string[] args) {
         FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, 
            FileAccess.ReadWrite);
         
         for (int i = 1; i <= 20; i++) {
            F.WriteByte((byte)i);
         }
         F.Position = 0;
         for (int i = 0; i <= 20; i++) {
            Console.Write(F.ReadByte() + " ");
         }
         F.Close();
         Console.ReadKey();
      }
   }
}

C# integer literals

using System;

namespace Demo {
    
   class Program {
       
      static void Main(string[] args) {
        
        // integer literal
        int a = 200;
        Console.WriteLine(a);

      }
   }
}

C# Type conversion

using System;

namespace Demo {
    
   class Program {
       
      static void Main(string[] args) {
         double d = 8745.97; 
         int x;
         
         x = (int)d;
         Console.WriteLine(x);
         Console.ReadKey();
      }
   }
}

C# read input from console

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string val;
      Console.Write("Enter Integer: ");
      val = Console.ReadLine();
        
      int a = Convert.ToInt32(val);
      Console.WriteLine("Your input: {0}",a);
   }
}

Advertisements
Loading...