0% found this document useful (0 votes)
18 views1 page

Dot Net Codes

Uploaded by

nammnew32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Dot Net Codes

Uploaded by

nammnew32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1. Default Constructor 2. Parametrized constructor 3. Destructor 4. Arrays 5. Base Class 6.

Exception Handling
public class vehicle public class person public class Vehicle public static void Main (string[] args) public class Animal Public Class Program
{ { { { { {
public void makesound() private string name; public void MakeSound() public void Eat() public static void main(string[] args)
{ private int age; { int [] array1 = new int[] { 5,6,7,8,9}; { Console.WriteLine("Animal Eats"); {
Console.WriteLine("Vehicle Makes Sound"); } } Console.WriteLine("Vehicle int [] array 2 = new int[5]; } try
} //parametrized constructor Makes Sound"); { int i=20; int resullt =i/0; }
public class program public person(string name, int age) } Console.WriteLine("Length of 1st array:" + Public Class Dog:Animal
{ { ~Vehicle() array1.Length); { catch(DivideByZeroException ex)
public static void main(string[] args) this.name=name; { Array.Sort(array1); Public void Eat() {
{ this.age=age; Console.WriteLine("Vehicle object PrintArray(array1); { Console.WriteLine("Error. Attempted divide by Zero");
vehicle car = new vehicle(); } removed."); base.Eat(); }
car.makesound(); } Array.Reverse(array1); Console.Writeline("Dog eats"); catch (Exception ex)
} } PrintArray(array1); } { Console.WriteLine("Error:{ex.Message}"); }
} // This will handle any other exception type
Array.Copy(array1,array2, array1.Length); }
} }

1. Properties 2. Indexers 3. Multiple Inheritance (Interface) 4. Method Overriding 5. Polymerphism


public class person public class MyCollection public interface IHumanwalk public class Animal { public class Animal {
{ { { void walk(); } public virtual void MakeSound() public virtual void MakeSound()
private string name; private string[] data = new string[3]; public interface IHumanswim { { Console.WriteLine("Animal Makes Sound");
private int age; // indexer { void swim(); } Console.WriteLine("Animal MakeSound"); }
public string this[int index] }} }
//property for accessing name field { public class Human : IHumanwalk , IHumanswim public class Dog : Animal public class Dog : Animal
public string Name get { return data[index]; } { { {
{ set { data[index] = value; } public void walk() public override void MakeSound() public override void MakeSound()
get { return name;} } { Console.WriteLine("Human Walks"); } { Console.WriteLine("Dog makes sound"); } {
set { name = value;} } } Console.WriteLine("Dogmakesound"); }
} Public class Program public void swim() public class Program }
//property for accessing age field { { Console.WriteLine("Human Swims"); } { public class Program
public int Age public static void Main(string[] args) } public Static void Main (string [] args) {
{ { Public class Program { public Static void Main (string [] args)
get { return age;} MyCollection collection = new MyCollection(); { Animal animal1 = new Animal(); {
set { age= value;} collection[0]='Ankit'; Public Static void Main ( string[] args) { animal1.MakeSound(); Animal animal1 = new Dog();
} Console.WriteLine(collection[0]); Human ankit = new Human(); Dog dog 1 = new Dog(); dog1.MakeSound(); animal1.MakeSound(); // Dogmake--
} } ankit.walk(); ankit.swim(); } }
} } } }
}

1. Struct 2. Enum 3. Abstract Class 4. Sealed Class 5. DELEGATES


public Struct Point public enum DaysOfWeek public abstract class Animal public Sealed class Animal //Declaration
{ { { { public delegate void SimpleDelegate();
Public int X; Sunday, public abstract void MakeSound() public abstract void MakeSound() class DelegateTest
Public int Y; Monday, { { {
} Tuesday, Console.WriteLine("Animal makes sound"); Console.WriteLine("Animal makes sound"); static void main (string[] args)
public class Program Wednesday, } } {
{ Thursday, public class Dog : Animal public class Dog : Animal // error //Installation
public static void main (string[] args) Friday, { { SimpleDelegate d=new SimpleDelegate(MyFunc);
{ Saturday public override void MakeSound() }
} { public class program // Invocation
Point point1; public class Program Console.WriteLine("Dog Barks"); { d();
point1.X=10; { } public static void Main(string[] args) }
point1.Y=20; public static void Main(string[] args) public class Program { public static void MyFunc()
Console.WriteLine("Coordinates: ({0}, {1}) ", { { Animal animal1 = new Animal(); {
point1.X, point1.Y ); DaysOfWeek today = DaysOfWeek.Monday; Public Static void Main(string[] args) animal1.MakeSound(); Console.WriteLine("I was called by a delegate");
Console.WriteLine("Today is {0}. ". today); { } }
} } Animal animal1 = new Dog(); animal1.MakeSound(); } }
} } }}

1. Events 2. Partial Class 3. Collection & Generics 4. LINQ & Lamda expressions 5. Asynchronous Programming
public class Button File 1: Part1.cs (Note: generics ma list matra rakhne) public class program
{ public partial class Animal using System using System {
public event EventHandler Click; { using System.Collections.Generic using System.Collections.Generic public static async Task Main(0
public void Dog() public class program public class Program {
public void ClickButton() { { { Console.WriteLine("Start");
{ Console.WriteLine("Dog Barks"); public static void main(string[] args) public static void main(string[] args)
Console.WriteLine("Button clicked!"); } { { //call the asyncronous method & use await to
Click?.Invoke(this, EventArgs.Empty); } //list example wait for it to complete
} File 2: Part2.cs List<string> names = new List<string> (); List <int> numbers = new List <int> {1,7,3,9,5,2};
} public partial class Animal names.Add("Ankit"); await DoSomethingAsync();
{ names.Add("Kushal"); // LINQ Query to filter even numbers Console.WriteLine("End");
public class Program public void Cat() names.Add("Sulab"); var evenNumbers = }
{ { Console.WriteLine("List of names:"); numbers.where(num=>num%2 ==0);
public static void Main(string[] args) Console.WriteLine("Cat Meows"); foreach (string name in names) public static async Task DoSomethingAsync()
{ } { Console.WriteLine("Even numbers:"); {
Button button1 = new Button(); } Console.WriteLine(name); foreach( var num in evenNumbers) Console.WriteLine("Async Method Started");
File 3 : Program.cs } {
button1.Click += (s, e) => public class Program //dictionary example Console.WriteLine(num); //simulate an asynchronous operation (eg. a
Console.WriteLine("Button was clicked!"); { Dictionary<string,int> ages = new } delay) using Task.Delay
public static void Main(string[] args) Dictionary<string,int>
button1.ClickButton(); { ages.Add("Ankit", 22); } await Task.Delay(2000); // pause for 2 sec
} Animal animal1 = new Animal(); ages.Add("Kushal",22); } Console.WriteLine("Astnc Method end");
} animal1.Dog(); animal1.Cat(); ages.Add("Sulab",23); }
} } }
} }

1. Roles 3. Securing Action Method in Controller 6. Model Binding 8. SQL INJECTION


using Microsoft.AspNetCore.Authorization; public class HomeController : Controller consider a simple model class for user registration using System; using System.Data.SqlClient;
using Microsoft.AspNetCore.Mvc; { class Program
public class HomeController : Controller [Authorize] public class user {
{ public IActionResult About() { static void Main()
[Authorize(Roles = "Admin")] { public string Username {get; set;} {
public IActionResult AdminDashBoard() ViewData["Message"]= "This is my about page"; public string Email {get; set;} Console.Write("Enter a username: "); string username = Console.ReadLine();
{ return View(); public int Age {get; set;}
Content("Welcome to the Admin Dashboard!"); } } string connectionString = "your_connection_string";
} string query = $"SELECT * FROM Users WHERE Username = '{username}'";
} 4. XSS (vulnerable code) 7. Model Validation
<div> @foreach(var comment in Model.Comments) public class user using (SqlConnection connection = new SqlConnection(connectionString))
2. Claims & Policies { { {
Services.AddAuthorization( options => <p> @ comment.Content </p> [Required(ErrorMessage="Username required")] connection.Open();
{ } public string Username {get; set;} SqlCommand command = new SqlCommand(query, connection);
options.Addpolicy("AdultOnly", policy => </div> SqlDataReader reader = command.ExecuteReader();
policy.RequireClaim("Age","18","19","20") [EmailAddress(ErrorMessage="Invalid email")]
); 5. SQL INJECTION (vulnerable code) public string Email {get; set;} while (reader.Read())
}); pubic bool ValidateUser(string username, string {
passwpord) [Range(18,99,ErrorMessage="Age must be between 18 and 99")] string userId = reader["UserId"].ToString(); Console.WriteLine("User ID: " +
In this example, the "AdultOnly" Policy requires { public int Age {get; set;} userId);
the user to have an "Age" claim of 18, 19 or 20 string query= " SELECT COUNT(*) FROM USERS
to access the associated resources or actions. WHERE Username=' "+username+" ' AND Password= ' } }}}
"+password+" ' ";

You might also like