Skip to content

Commit 5c26b1e

Browse files
committed
Initial code
1 parent c35bb8a commit 5c26b1e

13 files changed

Lines changed: 1299 additions & 0 deletions

File tree

.vs/CsCodeExample/v14/.suo

42 KB
Binary file not shown.

CsCodeExample.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CsCodeExample", "CsCodeExample\CsCodeExample.csproj", "{FB389558-EE38-4C66-9F9D-D7CF5AC4CC21}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{FB389558-EE38-4C66-9F9D-D7CF5AC4CC21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{FB389558-EE38-4C66-9F9D-D7CF5AC4CC21}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{FB389558-EE38-4C66-9F9D-D7CF5AC4CC21}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{FB389558-EE38-4C66-9F9D-D7CF5AC4CC21}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

CsCodeExample/App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CsCodeExample
8+
{
9+
public class ComplexNumber // class
10+
{
11+
private double real; // Fields (usually hidden with properties)
12+
13+
public double Real // Properties ( Encapsulation, auto generated - Encapsulate field )
14+
{
15+
get { return real; }
16+
set { real = value; }
17+
}
18+
19+
public double Imag { get; set; } // Property with hidded field, PascalCase
20+
public static string Format { get { return "a + b * i"; } }
21+
22+
protected const double PI = 3.14; // keyword: const (just for example, in this class Math.PI is used because it has more precision)
23+
24+
public ComplexNumber() { } // class constructor, default - no parameters
25+
26+
public ComplexNumber(double real, double imag = 0) // constructor with 2 parameters, second has default value
27+
{
28+
Real = real;
29+
Imag = imag;
30+
}
31+
32+
public ComplexNumber Add(ComplexNumber input)
33+
{
34+
ComplexNumber result = new ComplexNumber();
35+
result.Real = Real + input.Real;
36+
result.Imag = Imag + input.Imag;
37+
return result;
38+
}
39+
40+
public static ComplexNumber Add(ComplexNumber a, ComplexNumber b)
41+
{
42+
ComplexNumber result = new ComplexNumber();
43+
result.Real = a.Real + b.Real;
44+
result.Imag = b.Imag + b.Imag;
45+
return result;
46+
}
47+
48+
public ComplexNumber Subtract(ComplexNumber input)
49+
{
50+
ComplexNumber result = new ComplexNumber();
51+
result.Real = Real - input.Real;
52+
result.Imag = Imag - input.Imag;
53+
return result;
54+
}
55+
56+
public ComplexNumber Multiply(ComplexNumber input)
57+
{
58+
ComplexNumber result = new ComplexNumber();
59+
result.Real = Real * input.Real - Imag * input.Imag;
60+
result.Imag = Real * input.Imag + Imag * input.Real;
61+
return result;
62+
}
63+
64+
public ComplexNumber Divide(ComplexNumber input)
65+
{
66+
ComplexNumber result = new ComplexNumber();
67+
result.Real = (Real * input.Real + Imag * input.Imag) / (input.Real * input.Real + input.Imag * input.Imag);
68+
result.Imag = (Real * input.Imag - Imag * input.Real) / (input.Real * input.Real + input.Imag * input.Imag);
69+
return result;
70+
}
71+
72+
public double Modul () {
73+
return Math.Sqrt(Real * Real + Imag * Imag);
74+
}
75+
76+
public ComplexNumber Pow(int n)
77+
{
78+
ComplexNumber result = new ComplexNumber(1, 0);
79+
if (n != 0)
80+
{
81+
for (int i = 0; i < Math.Abs(n); i++)
82+
result = result.Multiply(this);
83+
if (n < 0)
84+
result = new ComplexNumber(1, 0).Divide(result);
85+
}
86+
return result;
87+
}
88+
89+
public List<ComplexNumber> Nroot(int n) {
90+
List<ComplexNumber> result = new List<ComplexNumber>();
91+
double r = this.Modul();
92+
double f = Math.Atan(Imag / Real);
93+
if (Real == 0.0)
94+
{
95+
f = Math.PI/2;
96+
if (Imag < 0)
97+
f += Math.PI;
98+
}
99+
if (Real < 0)
100+
f += Math.PI;
101+
double p1 = Math.Pow(r, n);
102+
103+
for(var i = 0; i < n; i++) {
104+
double p2 = (f + 2 * i * Math.PI) / n;
105+
result.Add(new ComplexNumber(p1 * Math.Cos(p2), p1 * Math.Sin(p2)));
106+
}
107+
return result;
108+
}
109+
110+
public override string ToString() // keyword: override
111+
{
112+
return String.Format("({0:0.00}, {1:0.00})", Real, Imag);
113+
}
114+
115+
public string ToCSV() // CSV - Comma Separated Value
116+
{
117+
return String.Format("{0:0.00},{1:0.00}", Real, Imag);
118+
}
119+
120+
public virtual string ToDiffString() // keyword: virtual(can be overridden in child class)
121+
{
122+
string output = null;
123+
if(Imag == 0.0)
124+
output = String.Format("{0}", Real);
125+
else if(Imag > 0)
126+
output = String.Format("{0} + {1}i", Real, Imag);
127+
else if (Imag < 0)
128+
output = String.Format("{0} - {1}i", Real, -Imag);
129+
130+
return output;
131+
}
132+
133+
public static string GetDescription()
134+
{
135+
return "This class implements basic math function with complex numbers.";
136+
}
137+
}
138+
139+
public class MoreComplexNumber : ComplexNumber
140+
{
141+
public MoreComplexNumber() : base() { }
142+
143+
public MoreComplexNumber(double real, double imag = 0) : base(real, imag) { }
144+
145+
//public MoreComplexNumber(double real, double imag = 0)
146+
//{
147+
// Real = real;
148+
// Imag = imag;
149+
//}
150+
151+
public override string ToDiffString()
152+
{
153+
string output = null;
154+
if (Imag == 0.0)
155+
output = String.Format("{0}", Real);
156+
else if (Imag > 0)
157+
output = String.Format("{0} + {1} * i", Real, Imag);
158+
else if (Imag < 0)
159+
output = String.Format("{0} - {1} * i", Real, -Imag);
160+
161+
return output;
162+
}
163+
}
164+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Reflection;
8+
9+
namespace CsCodeExample
10+
{
11+
public enum Gender
12+
{
13+
Neutrum = 0,
14+
Masculinum = 1,
15+
Femininum = 2
16+
}
17+
18+
public enum Sex
19+
{
20+
[Description("Male desc")] // Attributes
21+
Male = 0,
22+
[Description("Female desc")]
23+
Female = 1
24+
}
25+
26+
public static class EnumEx
27+
{
28+
public static String GetDescription(this Enum value)
29+
{
30+
FieldInfo field = value.GetType().GetField(value.ToString()); // Reflection
31+
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
32+
return attribute == null ? value.ToString() : attribute.Description;
33+
}
34+
}
35+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CsCodeExample
8+
{
9+
public interface InitInterface
10+
{
11+
void Init(object[] array);
12+
}
13+
14+
public class GenericTest<T> where T : InitInterface, new()
15+
{
16+
public static List<T> Run()
17+
{
18+
List<T> list = new List<T>();
19+
var t = new T();
20+
t.Init(new object[2]);
21+
list.Add(t);
22+
return list;
23+
}
24+
}
25+
26+
public class A : InitInterface
27+
{
28+
public int X { get; set; }
29+
30+
public void Init(object[] array)
31+
{
32+
X = (int)array[0];
33+
}
34+
}
35+
36+
public class B : InitInterface
37+
{
38+
public double Y { get; set; }
39+
40+
public void Init(object[] array)
41+
{
42+
Y = (double)array[0];
43+
}
44+
}
45+
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CsCodeExample
8+
{
9+
public class Address
10+
{
11+
public string City { get; set; }
12+
public string Street { get; set; }
13+
public int Number { get; set; }
14+
}
15+
16+
public abstract class Human // keyword: abstract, can not be initialised, only inhereted
17+
{
18+
public DateTime BirthDate { get; set; }
19+
}
20+
21+
public class Person : Human
22+
{
23+
public string FirstName { get; set; }
24+
public string LastName { get; set; }
25+
public Sex PersonSex { get; set; }
26+
public Address HomeAddress { get; set; } // Composition
27+
28+
public int Age { get { return DateTime.Now.Year - BirthDate.Year; } }
29+
public string DisplayName { get { return FirstName + " " + LastName; } }
30+
31+
public override string ToString()
32+
{
33+
return String.Format("{0} {1}", FirstName, LastName);
34+
}
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CsCodeExample
8+
{
9+
abstract class Shape // keyword: abstract
10+
{
11+
public abstract double CalcArea();
12+
}
13+
14+
class Square : Shape
15+
{
16+
protected double side; // AccessModifier: protected
17+
18+
public Square(double side)
19+
{
20+
this.side = side; // keyword: this (to distinct field from local variable when they have same name)
21+
}
22+
23+
public override double CalcArea()
24+
{
25+
return side * side;
26+
}
27+
}
28+
29+
}

CsCodeExample/ClassExample/User.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CsCodeExample
8+
{
9+
public interface Employee
10+
{
11+
decimal Sallary { get; set; }
12+
string Company { get; set; }
13+
int YearsOfService { get; set; }
14+
15+
decimal CalculateSallaryCoeficiente();
16+
}
17+
18+
//public partial class User // keyword: partial
19+
public class User : Person, Employee // Inheritance Class (Parent - Child), Implements Interface
20+
{
21+
public string UserName { get; set; }
22+
public string Email { get; set; }
23+
protected string Password { get; set; }
24+
25+
public string Company { get; set; }
26+
public int YearsOfService { get; set; }
27+
public decimal Sallary { get; set; }
28+
29+
public void ResetPassword(string pass)
30+
{
31+
if (pass.Length < 8)
32+
throw new Exception("Password must have at least 8 characters.");
33+
Password = pass;
34+
}
35+
36+
public override string ToString()
37+
{
38+
return String.Format("{0} ({1})", base.ToString(), Company);
39+
}
40+
41+
public decimal CalculateSallaryCoeficiente()
42+
{
43+
if (YearsOfService < 10)
44+
return 1;
45+
else
46+
return 1.5m;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)