niemsh0007

Dot net

Jan 7th, 2023
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. Write a program in C# to implement conditional operator/ternary operator ?
  2. namespace ConsoleApp1
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             int x = 20, y = 10;
  9.  
  10.             var result = x > y ? "x is greater than y" : "x is less than y";
  11.  
  12.             Console.WriteLine(result);
  13.         }
  14.     }
  15. }
  16.  
  17. Write a program in C# to illustrate Operator Overloading.
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. namespace During_Vacation
  25. {
  26.     internal class UnaryOpOv
  27.     {
  28.         int a;
  29.         int b;
  30.         public UnaryOpOv()
  31.         {          
  32.         }
  33.         public UnaryOpOv(int x, int y)
  34.         {
  35.             a = x;
  36.             b = y;
  37.         }
  38.         public void display()
  39.         {
  40.             Console.WriteLine("value of a is: " + a);
  41.             Console.WriteLine("value of b is : " + b);
  42.         }
  43.         public static UnaryOpOv operator -(UnaryOpOv m)
  44.         {
  45.             UnaryOpOv myun = new UnaryOpOv();
  46.             myun.a = -m.a;
  47.             myun.b=-m.b;
  48.             return myun;
  49.         }
  50.     }
  51.     public class mainclass
  52.     {
  53.         public static void Main(string[] args)
  54.         {
  55.             UnaryOpOv m1 = new UnaryOpOv(2, 3);
  56.             m1.display();
  57.             UnaryOpOv m2 = new UnaryOpOv(4,5);
  58.             m2.display();
  59.             m2 = -m1;
  60.             m2.display();
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66. Write a program in C# to illustrate generics.
  67.  
  68.  
  69. using System;
  70. using System.Collections.Generic;
  71. using System.Linq;
  72. using System.Runtime.InteropServices;
  73. using System.Text;
  74. using System.Threading.Tasks;
  75.  
  76. namespace Practice_in__class
  77. {
  78.  
  79.     public class Mygeneric<T>
  80.     {
  81.  
  82.         // private data members
  83.         private T a;
  84.  
  85.         // using properties
  86.         public T Myproperty
  87.         {
  88.  
  89.             // using accessors
  90.             get
  91.             {
  92.                 return this.a;
  93.             }
  94.             set
  95.             {
  96.                 this.a = value;
  97.             }
  98.         }
  99.     }
  100.     internal class Rough_place
  101.     {
  102.         public static void Main(String[] args)
  103.         {
  104.             // instance of string type
  105.             Mygeneric<string> name = new Mygeneric<string>();
  106.             name.Myproperty = "My name is generic";
  107.  
  108.             // instance of float type
  109.             Mygeneric<float> myvalue = new Mygeneric<float>();
  110.             myvalue.Myproperty = 30.3F;
  111.             Console.WriteLine(name.Myproperty);
  112.             Console.WriteLine(myvalue.Myproperty);
  113.         }    
  114.  
  115.     }
  116.  
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment