C# Overview and Basics
C# Overview and Basics
Explain C#
Basic Syntax of C#
csharp
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, World!");
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/18
`using System;` → Imports the System namespace, which contains essential classes like `Console`.
`class Program {}` → Defines a class named `Program`.
`static void Main()` → Entry point of the program.
`Console.WriteLine("Hello, World!");` → Prints output to the console.
C# Code Basics
csharp
Control Flow
csharp
int x = 10;
if (x > 5) {
Console.WriteLine("x is greater than 5");
} else {
Console.WriteLine("x is 5 or less");
}
Loops
csharp
while (x > 0) {
Console.WriteLine(x);
x--;
}
Functions (Methods)
csharp
SayHello("Alice");
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/18
csharp
class Person {
public string Name;
Where is C# Used?
Web Development: ASP.NET Core for building web applications and APIs.
Game Development: Unity Engine.
Desktop Applications: Windows Forms, WPF.
Mobile Development: Xamarin.
Cloud Computing: Azure services.
AI & Machine Learning: ML.NET framework.
csharp
Where:
`<datatype>` specifies the type of data the variable holds (e.g., `int`, `double`, `string`).
`<variableName>` is the name of the variable (follows camelCase by convention).
`<value>` (optional) assigns an initial value to the variable.
csharp
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/18
Variables can be declared without assigning a value, but they must be assigned before use.
csharp
C# allows type inference using `var`, where the compiler automatically determines the type.
csharp
csharp
csharp
class Circle {
public readonly double Radius;
public Circle(double r) {
Radius = r; // Allowed in constructor
}
}
csharp
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/18
Multiple Variable Declarations
csharp
C# provides various ways to implement logical operations using conditional statements and logical
operators.
1. Conditional Statements
1.1 `if` Statement
csharp
if (number > 5) {
Console.WriteLine("Number is greater than 5");
}
csharp
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/18
csharp
2. Logical Operators
Operator Name Example (`x = 5, y = 10`)
`&&` Logical AND `(x > 3 && y < 15) → true`
` `
`!` Logical NOT `!(x > 3) → false`
csharp
csharp
int day = 3;
switch (day) {
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/18
4. Ternary Operator (`? :`)
A shorthand for `if-else` statements.
csharp
5. Short-Circuit Evaluation
Logical operators `&&` and `||` use short-circuiting, meaning they stop evaluating as soon as the result
is determined.
csharp
int x = 5;
if (x > 0 || SomeFunction()) {
Console.WriteLine("First condition is true, so SomeFunction() is NOT called.");
}
bool SomeFunction() {
Console.WriteLine("This function runs!");
return true;
}
Would you like examples of more complex logic, such as loops or LINQ filtering? 😊
C# Loop Syntax
Loops in C# allow code to execute repeatedly based on a condition. The main types of loops are:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/18
csharp
Example:
csharp
How it works:
Syntax:
csharp
while (condition) {
// Code to execute
}
Example:
csharp
int x = 3;
while (x > 0) {
Console.WriteLine($"x = {x}");
x--; // Decrement x
}
csharp
do {
// Code to execute
} while (condition);
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/18
Example:
csharp
int num = 0;
do {
Console.WriteLine($"num = {num}");
num++;
} while (num < 3);
Syntax:
csharp
Example:
csharp
csharp
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/18
csharp
6. Nested Loops
Loops inside loops.
csharp
C# follows the OOP (Object-Oriented Programming) paradigm, which includes the core principles:
✅ Encapsulation (Data Hiding)
✅ Inheritance (Code Reusability)
✅ Polymorphism (Method Overriding & Overloading)
✅ Abstraction (Hiding Implementation Details)
1. Classes & Objects
A class is a blueprint for creating objects.
An object is an instance of a class.
Syntax:
csharp
class Person {
// Fields (Private)
private string name;
// Constructor
public Person(string name) {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/18
this.name = name;
}
// Method
public void Greet() {
Console.WriteLine($"Hello, my name is {name}.");
}
}
csharp
class Car {
private string model;
// Usage
Car myCar = new Car();
myCar.Model = "Tesla"; // Using setter
Console.WriteLine(myCar.Model); // Output: Tesla
csharp
class Animal {
public void Speak() {
Console.WriteLine("Animal makes a sound");
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/18
// Dog class inherits from Animal
class Dog : Animal {
public void Bark() {
Console.WriteLine("Dog barks");
}
}
// Usage
Dog myDog = new Dog();
myDog.Speak(); // Inherited method
myDog.Bark(); // Child method
csharp
class Animal {
public virtual void Speak() {
Console.WriteLine("Animal speaks");
}
}
// Usage
Animal myAnimal = new Cat();
myAnimal.Speak(); // Output: Cat meows (Overridden method)
🔹 allows overriding.
🔹
`virtual`
`override` modifies the behavior in the subclass.
csharp
class MathOperations {
public int Add(int a, int b) {
return a + b;
}
// Usage
MathOperations math = new MathOperations();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/18
Console.WriteLine(math.Add(2, 3)); // Output: 5
Console.WriteLine(math.Add(2.5, 3.5)); // Output: 6.0
csharp
// Usage
Shape s = new Circle();
s.Draw(); // Output: Drawing a circle
csharp
interface IAnimal {
void Speak(); // No implementation
}
// Usage
IAnimal animal = new Dog();
animal.Speak(); // Output: Dog barks
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/18
csharp
class MathHelper {
public static double Pi = 3.14159;
// Usage
Console.WriteLine(MathHelper.Pi); // Output: 3.14159
Console.WriteLine(MathHelper.Square(4)); // Output: 16
Would you like an example of more advanced OOP concepts like dependency
injection or SOLID principles? 😊
convert to c#:
import requests
import math
import time
from IPython.display import clear_output
def leverage (entry_usdt, stop_usdt, capital, maximum_risk, coin_usdt, entry_fee = 0.0005, exit_fee =
0.0005):
return <SECRET FORMULA>
def profit (entry_usdt, close_usdt, capital, coin_usdt, leverage, direction, entry_fee = 0.0005, exit_fee
= 0.0005):
mult = 1
if direction == 1:
if close_usdt < entry_usdt :
mult = -1
elif direction == -1:
if close_usdt > entry_usdt:
mult = -1
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/18
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
class CryptoTrading
{
public static double Leverage(double entryUsdt, double stopUsdt, double capital, double maximumR
{
return SECRET_FORMULA;
}
public static double Profit(double entryUsdt, double closeUsdt, double capital, double coinUsdt,
{
int mult = 1;
if (direction == 1 && closeUsdt < entryUsdt)
{
mult = -1;
}
else if (direction == -1 && closeUsdt > entryUsdt)
{
mult = -1;
}
return SECRET_FORMULA;
}
if (response.IsSuccessStatusCode)
{
string jsonResponse = await response.Content.ReadAsStringAsync();
return JObject.Parse(jsonResponse);
}
else
{
return new JObject
{
{ "error", $"Request failed with status code {response.StatusCode}" },
{ "details", await response.Content.ReadAsStringAsync() }
};
}
}
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/18
Let me know if you need modifications or additional features! 🚀
mathematical formula in c#
In C#, you can use the `System.Math` library for mathematical operations. Here’s how you can write
formulas using C# syntax:
csharp
csharp
Trigonometric Functions
csharp
csharp
csharp
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 17/18
csharp
double Leverage(double entryUsdt, double stopUsdt, double capital, double maximumRisk, double coinUs
{
return (capital * maximumRisk) / ((entryUsdt - stopUsdt) * coinUsdt * (1 + entryFee + exitFee))
}
No, you do not need to import anything to use `Math` in C# because it is part of the System namespace,
which is included by default.
Example Usage
csharp
class Program
{
static void Main()
{
double sqrtValue = Math.Sqrt(25); // Square root
double powerValue = Math.Pow(2, 3); // 2^3
double piValue = Math.PI; // Constant Pi
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/18